Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E
2 seconds
256 megabytes
standard input
standard output
The BFS algorithm is defined as follows.
- 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.
- Extract a vertex vv from the head of the queue qq.
- Print the index of vertex vv.
- 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.
- If the queue is not empty, continue from step 2.
- 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 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.
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).
4
1 2
1 3
2 4
1 2 3 4
4
1 2
1 3
2 4
1 2 4 3
No
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;
}
2 seconds
256 megabytes
standard input
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.
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.
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.
4 4 2
2 3
1 2
1 3
1 4
0
0
3
3
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
0
0
0
3
3
4
4
5
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
0
0
0
0
3
4
4
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的更多相关文章
- Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) E.Trips
比赛的时候想到怎么做了 没调出来(感觉自己是个睿智) 给你N个点M条边,这M条边是一条一条加进去的 要求你求出加入每一条边时图中极大'K度'子图的大小 极大'K度'子图的意思是 要求出一个有尽量多的点 ...
- 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 ...
- Manthan, Codefest 18 (rated, Div. 1 + Div. 2) E bfs + 离线处理
https://codeforces.com/contest/1037/problem/E 题意 有n个人,m天,在第i天早上,x和y会成为朋友,每天晚上大家都要上车,假如一个人要上车那么他得有至少k ...
- 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 ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)
还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)
随便模拟下就过了qwq 然后忘了特判WA了QwQ #include <cstdio> #include <algorithm> #include <cstring> ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T3(贪心)
是一道水题 虽然看起来像是DP,但其实是贪心 扫一遍就A了 QwQ #include <cstdio> #include <algorithm> #include <cs ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)
题目要求很简单,做法很粗暴 直接扫一遍即可 注意结果会爆int #include <cstdio> #include <algorithm> #include <cstr ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T1(找规律)
就是找一下规律 但是奈何昨天晚上脑子抽 推错了一项QwQ 然后重新一想 A掉了QwQ #include <cstdio> #include <algorithm> #inclu ...
随机推荐
- SQL Server2012 T-SQL对分页的增强尝试
简介 SQL Server 2012中在Order By子句之后新增了OFFSET和FETCH子句来限制输出的行数从而达到了分页效果.相比较SQL Server 2005/2008的ROW_Numbe ...
- 怎么用eclipse生成jar文件?eclipse导出jar介绍
1 .我们先要增加jar需要的配置文件,选中项目的src目录,鼠标右键,选择 [New] -选择 [Folder] . 2. 输入META-INF 作为目录名称,点击[Finish] . 3. 选中刚 ...
- 基于Zabbix API文档二次开发与java接口封装
(继续贴一篇之前工作期间写的经验案例) 一. 案例背景 我负责开发过一个平台的监控报警模块,基于zabbix实现,需要对zabbix进行二次开发. Zabbix官方提供了Rest ...
- k8s集群之Docker安装镜像加速器配置与k8s容器网络
安装Docker 参考:https://www.cnblogs.com/rdchenxi/p/10381631.html 加速器配置 参考:https://www.cnblogs.com/rdchen ...
- Opencv竟然有中文资料
最近对于OpenCV看的较多,竟然不知不觉找到了一个中文网站,对于母语真的桥开心的嘻嘻 直方图均衡化: http://www.opencv.org.cn/opencvdoc/2.3.2/html/do ...
- python编码的初识
用途: 密码本:二进制 与 文字的对应关系 ASCII: 最早的密码本:二进制与 英文字母,数字,特殊字符的对应关系 格式: 01100001 a 01100010 b 字节数: 英文1个 ...
- 【传智播客】Libevent学习笔记(二):创建event_base
目录 00. 目录 01. 简介 02. 创建默认的event_base 03. 创建复杂的event_base 3.1 event_config_new函数 3.2 event_base_new_w ...
- ios之sqllite3简单使用
SQLite3是嵌入在iOS中的关系型数据库,对于存储大规模的数据很有效.SQLite3使得不必将每个对象都加到内存中. 基本操作: (1)打开或者创建数据库 sqlite3 *database; i ...
- CodeForces - 1105D Kilani and the Game(多源BFS+暴力)
题目: 给出一张游戏地图和每个玩家的位置,每次能移动的步数.p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束. 问在游戏结束后,每个玩家占领的格子的数 ...
- [LUOGU] [NOIP2017] P3960 列队
题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n \times mn×m 名学生,方阵的行 ...