zoukankan      html  css  js  c++  java
  • Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D.Valid BFS? (模拟)

    D. Valid BFS?
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The BFS algorithm is defined as follows.

    1. Consider an undirected graph with vertices numbered from 11 to nn. Initialize qq as a new queue containing only vertex 11, mark the vertex 11 as used.
    2. Extract a vertex vv from the head of the queue qq.
    3. Print the index of vertex vv.
    4. 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 qq.
    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 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 (1n21051≤n≤2⋅105) which denotes the number of nodes in the tree.

    The following n1n−1 lines describe the edges of the tree. Each of them contains two integers xx and yy (1x,yn1≤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,,ana1,a2,…,an (1ain1≤ai≤n) — the sequence to check.

    Output

    Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and "No" (quotes for clarity) otherwise.

    You can print each letter in any case (upper or lower).

    Examples
    input
    Copy
    4
    1 2
    1 3
    2 4
    1 2 3 4
    output
    Copy
    Yes
    input
    Copy
    4
    1 2
    1 3
    2 4
    1 2 4 3
    output
    Copy
    No
    Note

    Both sample tests have the same tree in them.

    In this tree, there are two valid BFS orderings:

    • 1,2,3,41,2,3,4,
    • 1,3,2,41,3,2,4.

    The ordering 1,2,4,31,2,4,3 doesn't correspond to any valid BFS order.

    分析:根据题意,进行第一个样例的模拟,我们可以发现,既然1一定是第一个数,就从1开始扩展,它连接2,3,那么后面一定是2,3,所以需要扫描2,3位置,然后2连接4,扫描4位置

    我们可以发现即使有很多数,扫描过程也是从前往后线性进行的,所以可以直接根据题意进行模拟,先标记该位置连接的数,再扫描具体的区间看是否相符。

    代码如下:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    using namespace std;
    #define INF 0x3f3f3f3f
    const int MAXN=2e5+100;
    int n,x,y;
    vector<int>V[MAXN];
    int vis[MAXN];
    int a[MAXN];
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n-1;i++)
        {
          scanf("%d%d",&x,&y);
          V[x].push_back(y);
          V[y].push_back(x);
        }
         for(int i=1;i<=n;i++)
         scanf("%d",&a[i]);
        vis[1]=1;
        int p=2;
        bool flag=true;
        if(a[1]!=1)
        flag=false;
         int len=V[a[1]].size();
        for(int i=1;i<=n;i++)
        {
           for(int j=0;j<V[a[i]].size();j++)
            {
                int x=V[a[i]][j];
                vis[x]=1;
            }
           for(int j=p;j<p+len&&j<=n;j++)
           {
               if(!vis[a[j]])
               {
                   flag=false;
                   break;
               }
           }
            p=p+len;
            len=V[a[i+1]].size()-1;
           if(flag==false)
           break;
        }
        if(flag==true)printf("Yes
    ");
        else printf("No
    ");
        return 0;
    }
  • 相关阅读:
    毫秒级从百亿大表任意维度筛选数据,是怎么做到的...
    编译时异常和运行时异常的区别
    ajax同步与异步的区别
    jdk、jre、jvm三者联系
    java可变参数
    String 堆内存和栈内存
    构造方法
    为什么成员变量不用先初始化
    Javascript 创建对象方法的总结
    Java四种读取和创建XML文档的例子教程
  • 原文地址:https://www.cnblogs.com/a249189046/p/9643189.html
Copyright © 2011-2022 走看看