D. Valid BFS

题目链接:https://codeforces.com/contest/1037/problem/D

D. Valid BFS?

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The BFS algorithm is defined as follows.

  1. 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.
  2. Extract a vertex vv from the head of the queue qq.
  3. Print the index of vertex vv.
  4. 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.
  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.

Input

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.

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

Copy

4
1 2
1 3
2 4
1 2 3 4

output

Copy

Yes

input

Copy

4
1 2
1 3
2 4
1 2 4 3

output

Copy

No

Note

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.

题目大意:给出一颗树,节点1为根节点,再给出一段序列,如果这个序列能用 bfs 遍历顺序得出,输出Yes 否则,输出No;

要训练将转换问题的能力,如果按照所给的序列以个一个比对,可能很难实现,

代码代码代码代码代码代码代码代码代码代码代码代码代码

我给你讲啊,这个人,大佬--->

 

#include<iostream>
#include<queue>
#include<vector>
#include<set>
using namespace std;
int  const  MAXN=2*1e5+10;
queue<int> q;
vector< set<int > > vec(MAXN);//定义一个vector数组,里面是set<int>类型;
int dis[MAXN];
int main(){
    int n;
    cin>>n;
    int a,b;
    for(int i=0;i<n-1;i++){
        cin>>a>>b;
        vec[a].insert(b);//无向图存图;
        vec[b].insert(a);
    }
    for(int i=0;i<n;i++){
        cin>>dis[i];
    }
    q.push(1);
    if(dis[0]!=1){//开头一定是1,如果不是1,就没有必要往下进行了;
        cout<<"No"<<endl;
        return 0;
    }
    int i=1;
    while(!q.empty()){
        int s=q.front();//取出队列中的元素;
        q.pop();
        //vec[s].find(dis[i] 表示以点s为父节点遍历所有他的孩子,再将每个孩子放入到队列中;
        while(i<n && vec[s].find(dis[i])!=vec[s].end()){
            q.push(dis[i]);
            i++;//i一直往前走,把dis[i]的值放入队列之中,
        }
    }
    if(i==n){//如果i能够一直走到头,说明给出的序列满足bfs的定义方式;
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
    return 0;
}

 

《人工智能导论》作业2 对应教学内容:Lecture2:Solving Problems by Uninformed Searching 题目:(参考AIMA(3rd edition)习题3.9)传教士与野人问题求解 三个传教士和三个野人在河的一边,有一条能1载一个人或两个人的船。请设法使所有人都渡到河的另一边。为了防止野人攻击传教士,无论在河的哪一边,都不能让野人人数超过传教士人数,否则野人就会攻击传教士。假设传教士和野人都会划船,请为传教士和野人规划一个安全的过河方案。这个问题在AI领域很有名,它是第一个从分析的观点探讨问题形式化的论文的主题(Amarel,1968)。 作业要求: 1.将传教士与野人问题形式化表述为一个搜索问题。明确定义状态表示、初始状态、目标状态、可行的动作集合、状态转移函数、动作代价,并分析该问题的状态空间。 2.针对该问题,设计并实现广度优先搜索(BFS)算法(建议用Python): a)采用队列存储等待扩展的节点; b)用一个集合存储已访问过的状态,避免重复访问; c)当搜索到目标状态时,输出从初始状态到目标状态的状态转移序列,统计搜索空间中被访问过的状态总数; d)若搜索完整个状态空间也未找到解,则输出"No solution"。 3.针对该问题,设计并实现深度优先搜索(DFS)算法(建议用Python): a)采用栈存储等待扩展的节点; b)用一个集合存储已访问过的状态; c)搜索过程中,选择第一个遇到的可行动作进行扩展,直到达到目标状态,或遍历所有状态; d)当搜索到目标状态时,输出从初始状态到目标状态的状态转移序列,统计搜索空间中被访问过的状态总数; e)若搜索完整个状态空间也未找到解,则输出"No solution"。 4.分析BFS和DFS在该问题上的时间、空间复杂度,并比较它们搜索过程中扩展节点数量的差异。你认为哪种算法更适合求解该问题? 5.讨论采用无信息搜索算法求解该问题可能存在的limitation。你有什么优化搜索效率的思路吗? 评分标准: 1.问题形式化表示是否准确、完整(20分); 2.BFS、DFS算法设计与实现的正确性、规范性、注释是否充分等(30分); 3.复杂度分析是否合理,算法比较是否有理有据,对无信息搜索局限性的认识和改进思路(30分); 4.报告的清晰度、逻辑性和表达能力,算法运行情况,提交材料的完整性和规范性(20分)。 提交方式: 以压缩包形式(命名:班级+学号+姓名+A2)提交如下文件: 1.问题形式化表述、算法设计(伪代码)、算法分析、比较和讨论的文档(PDF或word格式); 2.实现BFS和DFS的源代码。
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值