• CF1037D Valid BFS?


    The BFS algorithm is defined as follows.

    1. Consider an undirected graph with vertices numbered from 1

    to n. Initialize q as a new queue containing only vertex 1, mark the vertex 1

    • as used.
    • Extract a vertex v
    from the head of the queue q
    • .
    • Print the index of vertex v
    • .
    • Iterate in arbitrary order through all such vertices u
    that u is a neighbor of v and is not marked yet as used. Mark the vertex u as used and insert it into the tail of the queue q
    1. .
    2. If the queue is not empty, continue from step 2.
    3. Otherwise finish.

    Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.

    In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex 1

    . The tree is an undirected graph, such that there is exactly one simple path between any two vertices.

    Input

    The first line contains a single integer n

    (1n2105

    ) which denotes the number of nodes in the tree.

    The following n1

    lines describe the edges of the tree. Each of them contains two integers x and y (1x,yn

    ) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree.

    The last line contains n

    distinct integers a1,a2,,an (1ain

    ) — the sequence to check.

    Output

    Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and "No" (quotes for clarity) otherwise.

    You can print each letter in any case (upper or lower).

    Examples
    Input
    Copy
    4
    1 2
    1 3
    2 4
    1 2 3 4
    Output
    Copy
    Yes
    Input
    Copy
    4
    1 2
    1 3
    2 4
    1 2 4 3
    Output
    Copy
    No
     
    题意:给定一个bfs序列,看是不是合法的;
    我们建立边的时候,可以按边端点出现的顺序先排序,这样才可能会出现题目所给的序列;
    如果不一样,那么就是不合法的;
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize("O3")
    using namespace std;
    #define maxn 200005
    #define inf 0x3f3f3f3f
    #define INF 9999999999
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-3
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    inline ll rd() {
    	ll x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    ll sqr(ll x) { return x * x; }
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    
    
    ll qpow(ll a, ll b, ll c) {
    	ll ans = 1;
    	a = a % c;
    	while (b) {
    		if (b % 2)ans = ans * a%c;
    		b /= 2; a = a * a%c;
    	}
    	return ans;
    }
    
    int vis[maxn];
    int n;
    int pre[maxn];
    queue<int>q;
    int a[maxn];
    vector<int>vc[maxn];
    int b[maxn];
    
    bool cmp(int x, int y) {
    	return pre[x] < pre[y];
    }
    
    void bfs() {
    	q.push(1); vis[1] = 1;
    	int cnt = 0;
    	while (!q.empty()) {
    		int u = q.front(); q.pop();
    		b[++cnt] = u;
    		for (int i = 0; i < vc[u].size(); i++) {
    			if (vis[vc[u][i]])continue;
    			int v = vc[u][i]; vis[v] = 1; q.push(v);
    		}
    	}
    }
    
    int main()
    {
    	//ios::sync_with_stdio(0);
    	rdint(n);
    	for (int i = 1; i < n; i++) {
    		int u, v; rdint(u); rdint(v);
    		vc[u].push_back(v); vc[v].push_back(u);
    	}
    	for (int i = 1; i <= n; i++) {
    		rdint(a[i]); pre[a[i]] = i;
    	}
    	for (int i = 1; i <= n; i++) {
    		sort(vc[i].begin(), vc[i].end(), cmp);
    	}
    	bfs();
    	for (int i = 1; i <= n; i++) {
    		if (a[i] != b[i]) { cout << "No" << endl; return 0; }
    	}
    	cout << "Yes" << endl;
        return 0;
    }
    
    EPFL - Fighting
  • 相关阅读:
    Linux下安装启动nginx的过程
    shell编程
    Linux中的权限管理
    Linux中的模式转换
    Linux入门2
    Linux入门1
    数据库的多表查询及左右连接
    Python命令行参数sys.argv[]
    Python 读取csv文件到excel
    高级技巧
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/9968156.html
Copyright © 2020-2023  润新知