题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)
随便模拟下就过了qwq
然后忘了特判WA了QwQ
#include <cstdio> #include <algorithm> #include <cstring> #include <set> #include <queue> using namespace std; int n,u[400010],v[400010],fir[200010],nxt[400010],cnt=0,dep[200010],squ[200010]; set<int> sons[200010]; void addedge(int ui,int vi){ cnt++; u[cnt]=ui; v[cnt]=vi; nxt[cnt]=fir[ui]; fir[ui]=cnt; } void dfs(int u,int f){ for(int i=fir[u];i;i=nxt[i]){ if(v[i]==f) continue; dep[v[i]]=dep[u]+1; sons[u].insert(v[i]); dfs(v[i],u); } } int main(){ scanf("%d",&n); for(int i=1;i<=n-1;i++){ int a,b; scanf("%d %d",&a,&b); addedge(a,b); addedge(b,a); } queue<int> q; for(int i=1;i<=n;i++) scanf("%d",&squ[i]); dep[1]=1; dfs(1,0); q.push(squ[1]); for(int i=1;i<=n;){ if (q.empty()&&i==n) { break; } else if(q.empty()&&i!=n){ printf("No\n"); return 0; } int x=q.front(); q.pop(); for(int j=1;j<=sons[x].size();j++){ if(sons[x].count(squ[i+j])){ q.push(squ[i+j]); continue; } else{ printf("No\n"); return 0; } } i+=sons[x].size(); } printf("Yes\n"); return 0; }