0
点赞
收藏
分享

微信扫一扫

CF1037D Valid BFS?

敬亭阁主 2022-05-27 阅读 55

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

 

题意:给定一个bfs序列,看是不是合法的;我们建立边的时候,可以按边端点出现的顺序先排序,这样才可能会出现题目所给的序列;如果不一样,那么就是不合法的;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#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 pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair 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];
queueq;
int a[maxn];
vectorvc[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

举报

相关推荐

0 条评论