【CodeForces】CF1037D Valid BFS?

题目地址:

https://www.luogu.com.cn/problem/CF1037D

题面翻译:
给定一个 n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n(1\leq n \leq 2\cdot10^5) n(1n2105)个节点的树的 n − 1 n-1 n1条边和这棵树的一个 B F S BFS BFS a 1 , a 2 , … , a n a_1,a_2,\dots,a_n a1,a2,,an,判断这个 B F S BFS BFS序是否是一个从节点 1 1 1开始的合法 B F S BFS BFS序,若合法则输出 Y e s Yes Yes,否则输出 N o No No

题目描述:
The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 1 1 1 to n n n . Initialize q q q as a new queue containing only vertex 1 1 1 , mark the vertex 1 1 1 as used.
  2. Extract a vertex v v v from the head of the queue q q q .
  3. Print the index of vertex v v v .
  4. Iterate in arbitrary order through all such vertices u u u that u u u is a neighbor of v v v and is not marked yet as used. Mark the vertex $ u $ as used and insert it into the tail of the queue q q q .
  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 1 1 1 . The tree is an undirected graph, such that there is exactly one simple path between any two vertices.

输入格式:
The first line contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105 ) which denotes the number of nodes in the tree.

The following n − 1 n - 1 n1 lines describe the edges of the tree. Each of them contains two integers x x x and y y y ( 1 ≤ x , y ≤ n 1 \le x, y \le n 1x,yn ) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree.

The last line contains n n n distinct integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain ) — the sequence to check.

输出格式:
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).

可以直接BFS模拟。首先 1 1 1一定是一开始就会遍历到的点,接下来从 1 1 1开始BFS,首先将出队的点的所有未遍历过的点都放入一个哈希表,然后看一下序列,按序列里的次序遍历哈希表里的点(如果某个点不在哈希表里,则序列不成立),依次再加入队列以便下一轮BFS。这样直到所有点都遍历完成,如果一直没有问题,则说明序列成立。代码如下:

#include <iostream>
#include <cstring>
#include <queue>
#include <unordered_set>
using namespace std;

const int N = 2e5 + 10, M = 2 * N;
int n;
int h[N], e[M], ne[M], idx;
bool vis[N];
queue<int> q, o;

#define add(a, b) e[idx] = b, ne[idx] = h[a], h[a] = idx++

bool bfs() {
  vis[1] = true;
  q.push(1);
  if (o.front() != 1) return false;
  o.pop();
  while (q.size()) {
    unordered_set<int> st;
    int t = q.front(); q.pop();
    for (int i = h[t]; ~i; i = ne[i]) {
      int v = e[i];
      if (vis[v]) continue;
      vis[t] = true;
      st.insert(v);
    }

    while (st.size()) {
      int t = o.front();
      if (!st.count(t)) return false;
      st.erase(t);
      q.push(t);
      o.pop();
    }
  }
  return true;
}

int main() {
  memset(h, -1, sizeof h);
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }

  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    o.push(x);
  }

  puts(bfs() ? "Yes" : "No");
}

时空复杂度 O ( n ) O(n) O(n)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值