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]);
}

Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E的更多相关文章

  1. Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) E.Trips

    比赛的时候想到怎么做了 没调出来(感觉自己是个睿智) 给你N个点M条边,这M条边是一条一条加进去的 要求你求出加入每一条边时图中极大'K度'子图的大小 极大'K度'子图的意思是 要求出一个有尽量多的点 ...

  2. Manthan, Codefest 18 (rated, Div. 1 + Div. 2) F 单调栈 + 贡献 + 计数

    https://codeforces.com/contest/1037/problem/F 题意 function z(array a, integer k): if length(a) < k ...

  3. Manthan, Codefest 18 (rated, Div. 1 + Div. 2) E bfs + 离线处理

    https://codeforces.com/contest/1037/problem/E 题意 有n个人,m天,在第i天早上,x和y会成为朋友,每天晚上大家都要上车,假如一个人要上车那么他得有至少k ...

  4. Manthan, Codefest 18 (rated, Div. 1 + Div. 2) C D

    C - Equalize #include<bits/stdc++.h> using namespace std; using namespace std; string a,b; int ...

  5. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)

    还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...

  6. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)

    随便模拟下就过了qwq 然后忘了特判WA了QwQ #include <cstdio> #include <algorithm> #include <cstring> ...

  7. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T3(贪心)

    是一道水题 虽然看起来像是DP,但其实是贪心 扫一遍就A了 QwQ #include <cstdio> #include <algorithm> #include <cs ...

  8. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)

    题目要求很简单,做法很粗暴 直接扫一遍即可 注意结果会爆int #include <cstdio> #include <algorithm> #include <cstr ...

  9. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T1(找规律)

    就是找一下规律 但是奈何昨天晚上脑子抽 推错了一项QwQ 然后重新一想 A掉了QwQ #include <cstdio> #include <algorithm> #inclu ...

随机推荐

  1. iOS 从相册中拿到 图片名 ,截取后缀,图片名

    //从路径中获得完整的文件名 (带后缀) 对从相册中取出的图片,视频都有效. NSString *fileName = [filePath lastPathComponent]; //获得文件名 (不 ...

  2. python中的get函数

    >>> a={1:'a',2:'b'}>>> print a.get(1)a>>> print a.get(3)None

  3. transform、transition 和 animation区别

    CSS3中和动画有关的属性有三个 transform.transition 和 animation.下面来一一说明:        transform   从字面来看transform的释义为改变,使 ...

  4. win10使用自带虚拟机没有Hyper-V场景

    开始咯~ 1.打开控制面板-程序和功能-启用或关闭Windows功能 2.发现下面并没有Hyper-v 真难受~~~   然后百度了一下原来是家庭版的win10没有.那就只能往下面看咯~ 3.在桌面添 ...

  5. upload 上传按钮组件 iview

    <!-- * @description 导入Excel * @fileName importExcel.vue * @author 彭成刚 * @date // :: * @version V1 ...

  6. while循环(break、continue)

    while循环 流程:判断条件是否为真,如果条件为真,执行代码块,然后再次判断条件是否为真,如果为真,执行代码块,直到条件判断为假,结束循环 格式 while  条件: 代码块(循环体) else:- ...

  7. PHP16 PHP访问MySQL

    学习要点 PHP访问MySQL配置 PHP访问MySQL函数介绍 足球赛程信息管理 PHP访问MySQL配置 PHP.ini配置文件确认以下配置已经打开 extension=php_mysql.dll ...

  8. Vue之数据绑定

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. CSS中列表项list样式

    CSS列表属性 属性 描述 list-style-属性 用于把所有用于列表的属性设置于一个声明中. list-style-image 将图象设置为列表项标志. list-style-position ...

  10. 关于DTCC数据库技术大会

    本次DTCC数据库技术大会是第9届了,这次大会虽然有不少公司的产品推介,总体来说还是有不少干货的. 专场较多,有选择地主要听了大数据实践跟流式计算这块.网易跟滴滴的分享比较不错. 了解到了现在大家是用 ...