- problem link:http://codeforces.com/contest/1037/problem/D
- 题意:给你一个具有n个节点的树,再给你一个节点的序列,问这个节点是不是按照BFS的规律进行排列的,如果是就输出Yes,否则输出No。
解题报告:
(1)题目要求的是时间限制是2s。但是其使用队列queue是用不了那么久的。内存限制是256M字节,下面的代码大约17M字节,绰绰有余。
(2)思路:首先将两条路是通路的用map标记。后来判断是否遵循BFS则用队列。代码很容易理解。如果不太理解队列的,下面有一个关于普通队列的基本应用的代码,可以运行试试,很容易理解。(1)普通队列的基本应用代码:
#include<iostream>
#include<queue>
using namespace std;
queue<int>q;
int main(){
ios::sync_with_stdio(false);cin.tie(0);
q.push(6);q.push(4);q.push(2);q.push(10);
while(!q.empty()){
cout<<q.front()<<endl;
q.pop();
}
}
(2)AC code:
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+6;
map<int,int>mp[N];
queue<int>q;
int main(){
ios::sync_with_stdio(false);cin.tie(0);
int n;cin>>n;
mp[0][1]=mp[1][0]=1;
for(int i=1;i<n;i++){
int x,y;cin>>x>>y;
mp[x][y]=mp[y][x]=1;
}
q.push(0);
for(int i=1;i<=n;i++){
int a;cin>>a;
//mp[q.front][a]=1则代表与a相邻的有元素,并把当前元素移出
while(!q.empty()&&!mp[q.front()].count(a))q.pop();
if(q.empty()){cout<<"No"<<endl;return 0;}
q.push(a);
}
cout<<"Yes"<<endl;
}