D. Valid BFS?

版权声明:小白一个,欢迎各位指错。 https://blog.csdn.net/qq_36424540/article/details/82461158

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

    (1≤n≤2⋅105

    ) which denotes the number of nodes in the tree.

    The following n−1

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

    ) — 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 (1≤ai≤n

    ) — 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

    Note

    Both sample tests have the same tree in them.

    In this tree, there are two valid BFS orderings:

    • 1,2,3,4
  • ,
  • 1,3,2,4
  • .

The ordering 1,2,4,3

doesn't correspond to any valid BFS order.

日常写挫:

 1. seq[i]  ,在程序中不知不觉自己改成了 i, 在上个游戏和里面也是

 2.边界处理  肯定是一开一闭,这样才能保证最后都能处理完成

3.条件判断     , 这种比较神奇的树

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
#define bug(x) printf("x:%d ***\n",x)

const int N=2e5+10;

vector<int> vec[N];

int seq[N];

int num[N],lev[N];
bool vis[N];

int main(){
   int n;
   scanf("%d",&n);
   rep(i,0,n-1){
      int u,v;
      scanf("%d %d",&u,&v);
       vec[u].push_back(v);
       vec[v].push_back(u);
   }
   rep(i,0,n)scanf("%d",&seq[i]);

   int wrong=0;
   if(seq[0]!=1)wrong=1;
   queue<int> q;
   q.push(1);
   while(!q.empty()){
	   int u=q.front();
	   q.pop();
	   vis[u]=1;
	   rep(j,0,vec[u].size()){
		   int v=vec[u][j];
		   if(vis[v])continue;
		   q.push(v);
		   lev[v]=lev[u]+1;
	   }
   }


   fill(vis,vis+n+1,0);
   
   int p=0;
   while(p<n){
	  int cnt=1;
	  while(p<n-1&&lev[seq[p]]==lev[seq[p+1]]){

		int n1=seq[p+1],n2=seq[p];
		if(num[n1]<num[n2]){ wrong=1;break;}
	
		int u=seq[p];
		rep(j,0,vec[u].size()){
			int v=vec[u][j];
			if(vis[v])continue;
			
			num[v]=cnt;
		}
		vis[u]=1;
		cnt++;
		p++;
	  }
	 
	  rep(i,0,vec[seq[p]].size()){
	  	int v=vec[seq[p]][i];
	  	if(vis[v])continue;
	  	num[v]=cnt;
	  
	  }
	  vis[seq[p]]=1;
	 
	  if(p<n-1&&lev[seq[p+1]]!=lev[seq[p]]+1){wrong=1;break;}
	  ++p;
	  
   }
   if(wrong)printf("No\n");
   else printf("Yes\n");
   return 0;
}

队友写的超级简单

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
vector<int> G[N];
int n, a[N], t[N], pos[N], mk[N];
queue<int> q;
bool vis[N];
/*
7
1 2
1 3
1 4
2 5
4 6
4 7
1 2 3 4 6 7 5
*/
int main(){
    scanf("%d", &n);
    int u, v;
    for(int i=1; i<n; i++){
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for(int i=1; i<=n; i++) scanf("%d", a+i), pos[a[i]]=i;

    bool ok=true;
    q.push(a[1]);
    vis[a[1]]=1;
    int cnt=0, cn=1;
    while(!q.empty()){
        int it=q.front();
        q.pop();
        ++cnt;
        if(a[cnt]!=it){
            ok=false;
            break;
        }
        int cc=0;
        for(int i=0; i<G[it].size(); i++){
            int to=G[it][i];
            if(!vis[to]){
                cc++;
                mk[pos[to]]=1;
                vis[to]=1;
            }
        }
        for(int i=cn+1; i<=cn+cc; i++){
            q.push(a[i]);
            if(mk[i]==0){
                ok=false;
                break;
            }
        }
        cn+=cc;
        if(ok==false) break;
    }
	if(a[1]!=1)ok=false;
    if(ok)  puts("Yes");
    else   puts("No");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/82461158