Valid BFS?

 CodeForces - 1037D 

The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from  as used.
  2. Extract a vertex .
  3. Print the index of vertex v.
  4. Iterate in arbitrary order through all such vertices .
  5. If the queue is not empty, continue from step 2.
  6. 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 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 ) which denotes the number of nodes in the tree.

The following 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 ) — 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

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

Note

Both sample tests have the same tree in them.

In this tree, there are two valid BFS orderings:

  • ,
  • .

The ordering 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('\n')
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\n",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\n",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
*/
View Code

相关文章:

  • 2021-05-29
  • 2022-02-26
  • 2021-07-05
  • 2021-12-24
  • 2022-02-17
猜你喜欢
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
相关资源
相似解决方案