Valid BFS?
CodeForces - 1037DThe BFS algorithm is defined as follows.
- Consider an undirected graph with vertices numbered from 11 to nn. Initialize qqas a new queue containing only vertex 1, mark the vertex 1 as used.
- Extract a vertex vv from the head of the queue q.
- Print the index of vertex v.
- 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 q.
- If the queue is not empty, continue from step 2.
- 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⋅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,…,an (1≤ai≤n) — the sequence to check.
Output
Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFStraversal of the given tree and "No" (quotes for clarity) otherwise.
You can print each letter in any case (upper or lower).
Examples
4
1 2
1 3
2 4
1 2 3 4
Yes
4
1 2
1 3
2 4
1 2 4 3
No
Note
Both sample tests have the same tree in them.
In this tree, there are two valid BFS orderings:
- 1,2,3,4,
- 1,3,2,4.
The ordering 1,2,4,3 doesn't correspond to any valid BFS order.
题意:给一棵树,和一串序列,问是否可以有一种从1开始 bfs的顺序可以使得bfs序为给出的序列
sol:复制来的题面给英语好的大佬看
按着题意大力模拟,按着序列的优先级确定加边的顺序,复杂度介于O(n)到O(n*logn)之间
Ps:序列第一个不是1直接No
#include <bits/stdc++.h> using namespace std; typedef int ll; inline ll read() { ll s=0; bool f=0; char ch=' '; while(!isdigit(ch)) { f|=(ch=='-'); ch=getchar(); } while(isdigit(ch)) { s=(s<<3)+(s<<1)+(ch^48); ch=getchar(); } return (f)?(-s):(s); } #define R(x) x=read() inline void write(ll x) { if(x<0) { putchar('-'); x=-x; } if(x<10) { putchar(x+'0'); return; } write(x/10); putchar((x%10)+'0'); return; } #define W(x) write(x),putchar(' ') #define Wl(x) write(x),putchar(' ') const int N=200005,M=400005; int n,Id[N],Pos[N]; vector<int>E[N]; struct Tree { int tot,Next[M],to[M],head[N]; inline void add(int x,int y) { Next[++tot]=head[x]; to[tot]=y; head[x]=tot; return; } bool Arr[N]; queue<int>Que; inline bool bfs(int S) { memset(Arr,0,sizeof Arr); int i,Now=0; Que.push(S); Arr[S]=1; while(!Que.empty()) { int x=Que.front(); Que.pop(); // printf("x=%d ",x); if(x==(Id[Now+1])) Now++; else return 0; for(i=head[x];i;i=Next[i]) if(!Arr[to[i]]) { Arr[to[i]]=1; Que.push(to[i]); } } return 1; } inline void Init() { tot=0; memset(head,0,sizeof head); memset(Arr,0,sizeof Arr); return; } }T; inline bool cmp(int x,int y) { return Pos[x]<Pos[y]; } int Duilie[N]; int main() { int i,j; T.Init(); R(n); for(i=1;i<n;i++) { int x=read(),y=read(); E[x].push_back(y); E[y].push_back(x); } for(i=1;i<=n;i++) { Pos[Id[i]=read()]=i; } for(i=1;i<=n;i++) { *Duilie=0; for(j=0;j<E[i].size();j++) Duilie[++*Duilie]=E[i][j]; sort(Duilie+1,Duilie+*Duilie+1,cmp); for(j=*Duilie;j>=1;j--) { // printf("%d %d ",i,Duilie[j]); T.add(i,Duilie[j]); } } (T.bfs(1))?puts("Yes"):puts("No"); return 0; } /* input 4 1 2 1 3 2 4 1 2 3 4 output Yes input 4 1 2 1 3 2 4 1 2 4 3 output No input 6 1 2 1 5 2 3 2 4 5 6 1 2 5 3 4 6 output Yes input 6 1 2 1 5 2 3 2 4 5 6 1 5 2 3 4 6 output No */