zoukankan      html  css  js  c++  java
  • [CF1037D] Valid BFS?

    问题描述

    The BFS algorithm is defined as follows.

    1. Consider an undirected graph with vertices numbered from 11 to n. Initialize q as a new queue containing only vertex 11, mark the vertex 11 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 u that 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 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 n (1≤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 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.

    输出格式

    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).

    样例输入输出

    样例输入1

    4
    1 2
    1 3
    2 4
    1 2 3 4

    样例输出1

    Yes

    样例输入2

    4
    1 2
    1 3
    2 4
    1 2 4 3

    样例输出2

    No

    提示

    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,3doesn't correspond to any valid BFS order.

    题目大意

    给你一棵树以及一个序列,判断该序列是否为这棵树的一个合法BFS序。

    解析

    BFS序的特征是会先列举出同一层的所有点,同时下一层的子结点要按照上一层的顺序列出。利用这个性质就可以接着道题了。记录每个点的儿子个数son[i],一个点的同一层的子节点必然在连续一段,长度为son[i]。每检查完一段就跳到当前父节点的下一个节点。

    代码

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #define N 200002
    using namespace std;
    int head[N],ver[N*2],nxt[N*2],l;
    int n,i,j,k,son[N],fa[N],a[N];
    int read()
    {
    	char c=getchar();
    	int w=0;
    	while(c<'0'||c>'9') c=getchar();
    	while(c<='9'&&c>='0'){
    		w=w*10+c-'0';
    		c=getchar();
    	}
    	return w;
    }
    void insert(int x,int y)
    {
    	l++;
    	ver[l]=y;
    	nxt[l]=head[x];
    	head[x]=l;
    }
    void dfs(int x,int pre)
    {
    	fa[x]=pre;
    	for(int i=head[x];i;i=nxt[i]){
    		int y=ver[i];
    		if(y!=pre){
    			son[x]++;
    			dfs(y,x);
    		}
    	}
    }
    int main()
    {
    	n=read();
    	for(i=1;i<n;i++){
    		int u=read(),v=read();
    		insert(u,v);
    		insert(v,u);
    	}
    	for(i=1;i<=n;i++) a[i]=read();
    	dfs(1,0);
    	son[0]=1;
    	for(i=j=1;i<=n;i++,j++){
    		if(fa[a[i]]!=a[k]){
    			puts("No");
    			return 0;
    		}
    		if(j==son[a[k]]){
    			j=0;
    			k++;
    			while(son[a[k]]==0) k++;
    		}
    	}
    	puts("Yes");
    	return 0;
    }
    
  • 相关阅读:
    MySQL体系结构
    简单高效的代码部署方法
    笔试算法题(07):还原后序遍历数组 & 半翻转英文句段
    笔试算法题(06):最大连续子数组和 & 二叉树路径和值
    笔试算法题(05):转换BST为双向链表 & 查找栈中的最小元素
    笔试算法题(04):实现 string & memcpy & strcpy & strlen
    笔试算法题(03):最小第K个数 & 判定BST后序序列
    笔试算法题(02):N阶阶乘 & 双向循环链表实现
    笔试算法题(01):字符串倒置 & 八皇后问题
    chosen选择框加载数据
  • 原文地址:https://www.cnblogs.com/LSlzf/p/11674861.html
Copyright © 2011-2022 走看看