1037D - Valid BFS?(思维+bfs)

D. Valid BFS?

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 11 to nn. Initialize qq as a new queue containing only vertex 11, mark the vertex 11 as used.
  2. Extract a vertex vv from the head of the queue qq.
  3. Print the index of vertex vv.
  4. Iterate in arbitrary order through all such vertices uu that uu is a neighbor of vv and is not marked yet as used. Mark the vertex uu as used and insert it into the tail of the queue qq.
  5. If the queue is not empty, continue from step 2.
  6. 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 11. 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 nn (1≤n≤2⋅1051≤n≤2⋅105) which denotes the number of nodes in the tree.

The following n−1n−1 lines describe the edges of the tree. Each of them contains two integers xx and yy (1≤x,y≤n1≤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 nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤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,41,2,3,4,
  • 1,3,2,41,3,2,4.

The ordering 1,2,4,31,2,4,3 doesn't correspond to any valid BFS order.

题意:给出一棵树,再给出一段序列吗,问这段序列是不是这棵树可行的bfs序

思路:先对构树的邻接表通过序列中的数的次序进行排序,再直接对树bfs,看其结果是否相同即可

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int size=2e5+5;
vector<int> G[size];
int arr[size];
int id[size];
bool vis[size];
bool bfs()
{
	vis[1]=true;
	int cnt=1;
	queue<int> q;
	q.push(1);
	while(!q.empty())
	{
		int t=q.front();
		q.pop();
		if(arr[cnt]!=t)
		{
			return false;
		}
		else cnt++;
		for(int i=0;i<G[t].size();i++)
		{
			if(!vis[G[t][i]])
			{
				vis[G[t][i]]=1;
				q.push(G[t][i]);
			}
		}
	}
	return true;
}
bool cmp(int a,int b)
{
	return id[a]<id[b];
}
int main()
{
	int n;
	scanf("%d",&n);
	fill(vis,vis+1+n,false);
	for(int i=1;i<=n;i++) G[i].clear();
	for(int i=1;i<n;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&arr[i]);
		id[arr[i]]=i;
	}
	for(int i=1;i<=n;i++) sort(G[i].begin(),G[i].end(),cmp);
	if(bfs()) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}

猜你喜欢

转载自blog.csdn.net/baiyifeifei/article/details/82377889
今日推荐