Codeforces 1037D Valid BFS? (BFS)

本文介绍了一种算法,用于验证给定序列是否为特定树结构的有效宽度优先搜索(BFS)遍历顺序。通过使用队列和集合数据结构,确保每个节点的子节点在其父节点被访问后按正确顺序加入队列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 1 to n. Initialize q as a new queue containing only vertex 1, mark the vertex 1 as used.
  2. Extract a vertex v from the head of the queue q.
  3. Print the index of vertex v.
  4. Iterate in arbitrary order through all such vertices uthat u is a neighbor of v and is not marked yet as used. Mark the vertex u as used and insert it into the tail of the queue 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. 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 n(1≤n≤2⋅10^5) which denotes the number of nodes in the tree.

The following n−1 lines describe the edges of the tree. Each of them contains two integers x and y (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 n distinct integers a1,a2,…,an (1≤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

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:

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

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

 

题意:给你一棵树和一个序列,问你是不是bfs序?

 

解题思路: 正常的bfs,当我们出一个父亲节点的时候,我们用set来维护它的儿子节点是不是都出现。如果都出现了,那么我们就按题目给的bfs序来把儿子节点压入到队列中。

 

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=2e5+10;
int n,fa[maxn],flag,du[maxn];
int a[maxn];
set<int> st1,st2;
vector<int> ve[maxn];
void bfs(){
	if(a[1]!=1){
		flag=0;return ;
	}
	queue<int> qu;
	while(!qu.empty()) qu.pop();
	qu.push(1);
	int st=2;
	while(!qu.empty()){
		int u=qu.front();qu.pop();
		int i,sum=0,te=du[u],k=st;
		st1.clear();st2.clear();
		for(i=0;i<te;i++){
			if(ve[u][i]!=fa[u]){
				st1.insert(a[k++]);
				st2.insert(ve[u][i]);
			}
		}
		if(st1!=st2){
			flag=0;return ;
		}
		for(i=0;i<te;i++) if(ve[u][i]!=fa[u]) qu.push(a[st++]),fa[a[st-1]]=u; 
		if(st==n){
			flag=1;
			return ;
		}
	}
}
int main(){
	int i,j;
	while(scanf("%d",&n)!=EOF){
		for(i=1;i<=n;i++) ve[i].clear();
		int u,v;mem(du,0);
		for(i=1;i<n;i++){
			scanf("%d%d",&u,&v);
			ve[u].push_back(v);ve[v].push_back(u);
			du[u]++;du[v]++;
		}
		for(i=1;i<=n;i++) scanf("%d",&a[i]);
		flag=1;
		bfs();
		if(flag) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

 

 

 

### Codeforces Problem 1014D 解答与解释 当前问题并未提供关于 **Codeforces Problem 1014D** 的具体描述或相关背景信息。然而,基于常见的竞赛编程问题模式以及可能涉及的主题领域(如数据结构、算法优化等),可以推测该问题可能属于以下类别之一: #### 可能的解法方向 如果假设此问题是典型的计算几何或者图论类题目,则通常会涉及到如下知识点: - 图遍历(DFS 或 BFS- 贪心策略的应用 - 动态规划的状态转移方程设计 由于未给出具体的输入输出样例和约束条件,这里无法直接针对Problem 1014D 提供精确解答。但是可以根据一般性的解决思路来探讨潜在的方法。 对于类似的复杂度较高的题目,在实现过程中需要注意边界情况处理得当,并且要充分考虑时间效率的要求[^5]。 以下是伪代码框架的一个简单例子用于说明如何构建解决方案逻辑流程: ```python def solve_problem(input_data): n, m = map(int, input().split()) # 初始化必要的变量或数组 graph = [[] for _ in range(n)] # 构建邻接表或其他形式的数据表示方法 for i in range(m): u, v = map(int, input().split()) graph[u].append(v) result = [] # 执行核心算法部分 (比如 DFS/BFS 遍历) visited = [False]*n def dfs(node): if not visited[node]: visited[node] = True for neighbor in graph[node]: dfs(neighbor) result.append(node) for node in range(n): dfs(node) return reversed(result) ``` 上述代码仅为示意用途,实际应用需依据具体题目调整细节参数设置及其功能模块定义[^6]。 #### 关键点总结 - 明确理解题意至关重要,尤其是关注特殊测试用例的设计意图。 - 对于大规模数据集操作时应优先选用高效的时间空间性能表现良好的技术手段。 - 结合实例验证理论推导过程中的每一步骤是否合理有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值