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 ...
随机推荐
- Oracle PL/SQL 编程手册(SQL大全)
一.SQLPLUS 1引言 SQL命令 以下17个是作为语句开头的关键字: alterdroprevoke auditgrantrollback* commit*inse ...
- 基于Ubuntu 14.04 LTS编译Android4.4.2源代码
转载自:雨水:http://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS编译Android4.4.2源代码 ...
- WebSocket 学习笔记
WebSocket 学习笔记 来自我的博客 因为项目原因需要用到双工通信,所以比较详细的学习了一下浏览器端支持的 WebSocket. 并记录一些遇到的问题. 简介 WebSocket 一般是指浏览器 ...
- python-opencv 分离图片(视频)中的某一颜色物体
看代码: import cv2 as cv import numpy as np def separate_color(frame): cv.imshow("原图", frame) ...
- java指令详解
Java是通过java虚拟机来装载和执行编译文件(class文件)的,java虚拟机通过命令java option 来启动,-option为虚拟机参数,通过这些参数可对虚拟机的运行状态进行调整. 一. ...
- 深入Linux内核架构——锁与进程间通信
Linux作为多任务系统,当一个进程生成的数据传输到另一个进程时,或数据由多个进程共享时,或进程必须彼此等待时,或需要协调资源的使用时,应用程序必须彼此通信. 一.控制机制 1.竞态条件 几个进程在访 ...
- 特殊权限和facl
目 录 第1章 FACL访问控制 1 1.1 FACL简介 1 1.2 getfacl命令查看acl权限 1 1.3 setfacl设置文件的cal权限 1 1.4 批量添加a ...
- 使用Docker compose编排Laravel应用
前言 Laravel官方开发环境推荐的是Homestead(其实就是一个封装好的Vagrant box),我感觉这个比较重,于是自己用Docker compose编排了一套开发环境,在这里分享下. 环 ...
- 「LibreOJ β Round #3」绯色 IOI(抵达)
[题解] 我们可以发现叶子节点的关联点一定是它的父亲节点,那么我们dfs一遍就可以求出所有节点的关联点,或者判断出无解. 对于每个点i,它的关联点u的危险度肯定比它连接的其他点vi的危险度小,我们从u ...
- 杭电 4907 Task schedule ·
Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之 ...