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 ...
随机推荐
- Intel手册 Chapter23 VMX的简单介绍
23.2 虚拟机架构 1: VMX为处理器上的虚拟机定义了处理器级的支持.VMX主要支持两类,VMM和VM 2: VMM作为HOST可以完全控制处理器和其他平台硬件. 每个VM都支持一个栈,并且由O ...
- upupw nginx服务器 rewrite设置
最近开始尝试使用upupw的Nginx套件做开发,感觉还挺不错的,也遇到了一些问题,决定在这里记录一下,同时也希望可以帮助到一些人. 用习惯了Apache,改用Nginx之后会有些不适应,但是咬咬牙就 ...
- Python Linked List
上周日教导一个科班非技术的朋友学习 Python 编程.他的 Python 水平大概就是看了几篇短的 Python 介绍博客.会流程控制和全局函数编写. 具体教导思路是从自己实现一个链表出发,研究学习 ...
- Python3简明教程(六)—— 数据结构
简单的来说,数据结构(data structure)是计算机中存储.组织数据的方式.比如我们之前使用过的列表就是一种数据结构,在这里我们还会深入学习它.之前也有简单的介绍. 列表 >>&g ...
- 细说PHP-5.4 变量的类型
变量类型是指保存在该变量中的数据类型.计算机操作的对象是数据在计算编程语言世界里,每一个数据也都有它的类型,具有相同类型的数据才能彼此操作.例如书柜是装书用的.大衣柜是放衣服用的.保险柜是存放贵重物品 ...
- Python小记-- 读取当前目录下所有文件名
# -*- coding: utf-8 -*- import os def file_name(file_dir): with open("SelectAllFiles.txt", ...
- JS简单实现防抖和节流
一.什么是防抖和节流 Ps: 比如搜索框,用户在输入的时候使用change事件去调用搜索,如果用户每一次输入都去搜索的话,那得消耗多大的服务器资源,即使你的服务器资源很强大,也不带这么玩的. 1. 防 ...
- Ubuntu14.04环境下Qt5.5以上版本无法输入中文的解决教程
1.前言 由于Qt5.4之后对之前的Qt5版本不再二进制兼容,所以网上很多简单的旧的办法已经失效了,所以本教程的办法是重新编译fcitx-qt5,生成最新的libfcitxplatforminputc ...
- 约束RMQ
不知道为什么网上找不到太多相关的资料,所以写一个小总结,并附有能用的代码,抛砖引玉. 约束RMQ,就是RMQ区间必须满足两项之差最大为1,采用ST表的话,这时候有O(n)建表,O(1)查询的优秀复杂度 ...
- LeetCode(21)Merge Two Sorted Lists
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...