Valid BFS?
The BFS algorithm is defined as follows.
- 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.
- Extract a vertex vv from the head of the queue qq.
- Print the index of vertex vv.
- 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.
- 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.
The first line contains a single integer nn (1≤n≤2⋅1051≤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,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — 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).
4
1 2
1 3
2 4
1 2 3 4
Yes
4
1 2
1 3
2 4
1 2 4 3
No
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.
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 3e5+108; const ll mod = 1e9 + 7; int n; vector<int>adj[maxn],res; int seq[maxn],id[maxn]; int vis[maxn]; inline bool cmp(int s,int t){ return id[s]<id[t]; } int main() { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif scanf("%d",&n); for(register int i=1,u,v;i<n;++i){ scanf("%d%d",&u,&v); adj[u].emplace_back(v); adj[v].emplace_back(u); } for(register int i=1;i<=n;++i){ scanf("%d",&seq[i]); id[seq[i]]=i; } for(register int i=1;i<=n;++i){ sort(adj[i].begin(),adj[i].end(),cmp); } queue<int>q; // q.push(1); // vis[1]=1; // while(!q.empty()){ // queue<int>pre; // while(!q.empty()){ // int cur=q.front(); // q.pop(); // res.emplace_back(cur); // for(register int i=0;i<adj[cur].size();++i){ // if(!vis[adj[cur][i]]){ // pre.push(adj[cur][i]); // vis[adj[cur][i]]=1; // } // } // } // q=pre; // } q.push(1); vis[1]=1; while(!q.empty()){ int cur=q.front(); q.pop(); res.emplace_back(cur); for(register int i=0;i<adj[cur].size();++i){ if(!vis[adj[cur][i]]){ q.push(adj[cur][i]); vis[adj[cur][i]]=1; } } } for(register int i=1;i<=n;++i){ if(res[i-1]!=seq[i]){ puts("No"); return 0; } } puts("Yes"); return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!