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
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,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序。

解析  BFS序的特点就是 安层次的所以  i 的儿子是连续的 直接暴力判断当前的段是不是都是 i 的儿子。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
const int maxn=2e5+;
typedef long long ll;
vector<int> g[maxn];
map<pair<int,int>,int> ma;
int a[maxn];
int b[maxn];
int main()
{
int n;
cin>>n;
for(int i=;i<n-;i++)
{
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
ma[mp(u,v)]=;
ma[mp(v,u)]=;
}
for(int i=;i<=n;i++)
cin>>a[i];
if(a[]!=)
{
cout<<"No"<<endl;
return ;
}
int flag=,before=;
for(int i=;i<n;i++)
{
int num=;
for(int j=;j<g[a[i]].size();j++)
if(b[g[a[i]][j]]==)num++;
//cout<<i<<" "<<num<<endl;
for(int j=;j<num;j++)
{
if(before+j+>n)
break;
// cout<<a[i+j+1]<<" j"<<j<<endl;
if(ma[mp(a[i],a[before+j+])]!=)
{
flag=;
break;
}
//else
// b[a[i+j+1]]=1;
}
before=before+num;
//cout<<a[i]<<" "<<flag<<endl;
b[a[i]]=;
if(flag==)
break;
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
E. Trips
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.

We want to plan a trip for every evening of mm days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:

  • Either this person does not go on the trip,
  • Or at least kk of his friends also go on the trip.

Note that the friendship is not transitive. That is, if aa and bb are friends and bb and cc are friends, it does not necessarily imply that aa and cc are friends.

For each day, find the maximum number of people that can go on the trip on that day.

Input

The first line contains three integers nn, mm, and kk (2≤n≤2⋅105,1≤m≤2⋅1052≤n≤2⋅105,1≤m≤2⋅105, 1≤k<n1≤k<n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.

The ii-th (1≤i≤m1≤i≤m) of the next mm lines contains two integers xx and yy (1≤x,y≤n1≤x,y≤n, x≠yx≠y), meaning that persons xx and yy become friends on the morning of day ii. It is guaranteed that xx and yy were not friends before.

Output

Print exactly mm lines, where the ii-th of them (1≤i≤m1≤i≤m) contains the maximum number of people that can go on the trip on the evening of the day ii.

Examples
input

Copy
4 4 2
2 3
1 2
1 3
1 4
output

Copy
0
0
3
3
input

Copy
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
output

Copy
0
0
0
3
3
4
4
5
input

Copy
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
output

Copy
0
0
0
0
3
4
4
Note

In the first example,

  • 1,2,31,2,3 can go on day 33 and 44.

In the second example,

  • 2,4,52,4,5 can go on day 44 and 55.
  • 1,2,4,51,2,4,5 can go on day 66 and 77.
  • 1,2,3,4,51,2,3,4,5 can go on day 88.

In the third example,

  • 1,2,51,2,5 can go on day 55.
  • 1,2,3,51,2,3,5 can go on day 66 and 77.

解析 我们离线处理这个问题,先把每个点的入度和编号用pair保存起来 扔到set里面 ,每次把度数小于k的点删掉,与它相邻的点 j 度数减1 。把原来在set里的pair<du[j],j>删掉,再插入新的点

直到 set里的点的度数都大于k。set的size就是当前的答案。删掉当前的边,重复上面的操作。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
const int maxn=2e5+;
typedef long long ll;
typedef pair<int,int> pii;
int du[maxn];
set<int> g[maxn];
int b1[maxn],b2[maxn],ans[maxn],vis[maxn];
int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
set<pii> s;
for(int i=;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
b1[i]=x,b2[i]=y;
du[x]++,du[y]++;
g[x].insert(y),g[y].insert(x);
}
for(int i=;i<=n;i++)
s.insert(mp(du[i],i));
for(int i=m-;i>=;i--)
{
while(s.size()>=)
{
auto itt=(*s.begin());
if(itt.fi>=k)break;
for(auto it=g[itt.se].begin();it!=g[itt.se].end();it++)
{
int u=*it;
if(vis[u]==)continue;
s.erase(mp(du[u],u));
du[u]--;
s.insert(mp(du[u],u));
g[u].erase(itt.se);
}
s.erase(itt);
vis[itt.se]=;
}
ans[i]=s.size();
if(vis[b1[i]]==&&vis[b2[i]]==)
{
s.erase(mp(du[b1[i]],b1[i]));
du[b1[i]]--;
s.insert(mp(du[b1[i]],b1[i]));
g[b1[i]].erase(b2[i]);
s.erase(mp(du[b2[i]],b2[i]));
du[b2[i]]--;
s.insert(mp(du[b2[i]],b2[i]));
g[b2[i]].erase(b1[i]);
}
}
for(int i=;i<m;i++)
printf("%d\n",ans[i]);
}

最新文章

  1. EST
  2. mxnet实战系列(一)入门与跑mnist数据集
  3. Identity标识列
  4. ECMAScript 6 入门学习笔记(持续更新)
  5. android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索
  6. toString---&gt;转字符串
  7. Events and Responder Chain
  8. 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析
  9. vue入坑总结
  10. SQLServer之PRIMARY KEY约束
  11. qml: 模块定义与使用
  12. Java GC机制
  13. Hiberbate注解
  14. Dbutils 的JDBC链接
  15. 5-5 ES6的模块化的基本规则或特点
  16. MVC Filter中加入验证并跳转
  17. NodeJS写模块和引入模块的例子
  18. Java---页面之间传值跳转
  19. windows下使用c++调用redis
  20. python+selenium之字符串切割操作

热门文章

  1. Javaweb学习笔记4—Reuest&Response
  2. 做OJ项目时遇到的坑
  3. 工作中Git使用笔记
  4. MVC之在实例中的应用
  5. 联玛客(T 面试)
  6. 日常[系统]:Linux新人报到(吐槽%&amp;%……&amp;¥……%
  7. 解决Invalid bound statement (not found)(Mybatis的Mapper绑定问题)
  8. Manjaro安装SS客户端
  9. 「 Luogu P2420 」 让我们异或吧
  10. FFT NTT 模板