ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

「CF1037D」Valid BFS?

2019-10-26 21:51:12  阅读:270  来源: 互联网

标签:int BFS ++ tl Valid read rg include CF1037D


传送门
Luogu

解题思路

考虑直接模拟 \(\text{BFS}\) 的过程。
对于每一个节点的儿子,先遍历在输入序列中靠前的,判断 \(\text{BFS}\) 是否匹配即可。

细节注意事项

  • 注意一下输出格式

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while (!isdigit(c)) f |= (c == '-'), c = getchar();
    while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
    s = f ? -s : s;
}

const int _ = 200010;

int n, in[_], p[_];
int hd, tl, q[_], vis[_];
vector < int > G[_];

inline bool cmp(const int& x, const int& y) { return p[x] < p[y]; }

inline void bfs() {
    hd = tl = 0, q[++tl] = 1;
    while (hd < tl) {
        int u = q[++hd], X = G[u].size();
        vis[u] = 1;
        for (rg int i = 0; i < X; ++i) {
            int v = G[u][i];
            if (!vis[v]) q[++tl] = v;
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in", "r", stdin);
#endif
    read(n);
    for (rg int u, v, i = 1; i < n; ++i) read(u), read(v), G[u].push_back(v), G[v].push_back(u);
    for (rg int i = 1; i <= n; ++i) read(in[i]), p[in[i]] = i;
    for (rg int i = 1; i <= n; ++i) sort(G[i].begin(), G[i].end(), cmp);
    bfs();
    for (rg int i = 1; i <= n; ++i) if (in[i] != q[i]) return puts("No"), 0;
    puts("Yes");
    return 0;
}

完结撒花 \(qwq\)

标签:int,BFS,++,tl,Valid,read,rg,include,CF1037D
来源: https://www.cnblogs.com/zsbzsb/p/11745735.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有