【CF1037D】Valid BFS? 题解
Manthan, Codefest 18 (rated, Div. 1 + Div. 2)D题
核心思路
直接照题意模拟bfs的过程,顺序加入每个点与当前点的出边对应。用一个set维护最为直观。
nzr表示让我们不要学习这种Θ(nlogn)的做法。
完整代码
我的代码太丑了,大家看nzr神仙的代码吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include<bits/stdc++.h> using namespace std; const int N=200010; int n,m; struct edge { int to,next; }e[N<<1]; int head[N],cnt; void adde(int a,int b) { e[++cnt].to=b;e[cnt].next=head[a]; head[a]=cnt; } queue<int>q; int dep[N]; int a[N]; void out() { puts("No"); exit(0); } int now=1; void bfs() { dep[1]=1; q.push(1); while(!q.empty()) { int x=q.front(); q.pop(); int ct=0; for(int i=head[x];i;i=e[i].next) { int v=e[i].to; if(dep[v])continue; dep[v]=dep[x]+1;ct++; } for(int i=now+1;i<=now+ct;i++) if(!dep[a[i]])out(); else q.push(a[i]); now=now+ct; } } int main() { scanf("%d",&n); int u,v; for(int i=1;i<n;i++) scanf("%d%d",&u,&v),adde(u,v),adde(v,u); for(int i=1;i<=n;i++) scanf("%d",&a[i]); if(a[1]!=1)out(); bfs(); puts("Yes"); return 0; }
|