我真是一隻菜狗。。。。。。emm。。。c++
題意:spa
判斷一個從1開始的隊列是否能夠按照bfs的順序 進行遍歷。。必須從1開始。。。而後後邊依次是bfs順序code
解析:blog
看代碼能看懂吧。。。emm。。。就是把每一個有關係的用map標記一下隊列
而後bfs的時候 加一個循環進隊列就行了 emm。。/。it
#include <bits/stdc++.h> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d\n", a); #define plld(a) printf("%lld\n", a); #define pc(a) printf("%c\n", a); #define ps(a) printf("%s\n", a); #define MOD 2018 #define LL long long #define ULL unsigned long long using namespace std; const int maxn = 200005, INF = 0x7fffffff; int n; map<int, bool> w[maxn]; int a[maxn]; bool bfs() { queue<int> Q; int nex = 0; if(a[nex] != 1) return false; Q.push(a[nex]); nex++; while(!Q.empty()) { int u = Q.front(); Q.pop(); int v = a[nex]; while(w[u][v]) { Q.push(v); nex++; v = a[nex]; } } if(nex == n) return true; return false; } int main() { rd(n); int u, v; rep(i, 0, n-1) { rd(u), rd(v); w[u][v] = w[v][u] = 1; } rep(i, 0, n) rd(a[i]); if(n == 1) cout<< "Yes" <<endl; else if(bfs()) cout<< "Yes" <<endl; else cout<< "No" <<endl; return 0; }