[Codeforces 1037E] Trip
[题目链接]
http://codeforces.com/problemset/problem/1037/E
[算法]
首先离线 , 将问题倒过来考虑 , 转化为 : 每次删除一条边 , 此时最多有多少人参加旅行
假设所有点都选取 , 那么 ,如果一个点的度数 < k , 显然这个点不能选 , 我们需要删除它和所有与它相邻的边
显然 , 所有的点和边最多只会被删去一次 , 故时间复杂度为O(N + M)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ; int n , m , k;
int deg[MAXN],res[MAXN],x[MAXN],y[MAXN],q[MAXN];
bool visited[MAXN],del[MAXN];
vector< int > a[MAXN],b[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
int main()
{ read(n); read(m); read(k);
for (int i = ; i <= m; i++)
{
read(x[i]); read(y[i]);
a[x[i]].push_back(y[i]);
a[y[i]].push_back(x[i]);
b[x[i]].push_back(i);
b[y[i]].push_back(i);
deg[x[i]]++; deg[y[i]]++;
}
int l = , r = ,ans = n;
for (int i = ; i <= n; i++)
{
if (deg[i] < k)
{
visited[i] = true;
q[++r] = i;
ans--;
}
}
for (int i = m; i >= ; i--)
{
while (l <= r)
{
int u = q[l++];
for (unsigned j = ; j < a[u].size(); j++)
{
if (!del[b[u][j]])
{
deg[u]--;
deg[a[u][j]]--;
del[b[u][j]] = true;
if (!visited[a[u][j]] && deg[a[u][j]] < k)
{
ans--;
visited[a[u][j]] = true;
q[++r] = a[u][j]; }
}
}
}
res[i] = ans;
if (!del[i])
{
del[i] = true;
deg[x[i]]--;
deg[y[i]]--;
if (!visited[x[i]] && deg[x[i]] < k)
{
visited[x[i]] = true;
ans--;
q[++r] = x[i];
}
if (!visited[y[i]] && deg[y[i]] < k)
{
visited[y[i]] = true;
ans--;
q[++r] = y[i];
}
}
}
for (int i = ; i <= m; i++) printf("%d\n",res[i]); return ; }
[Codeforces 1037E] Trip的更多相关文章
- Codeforces A. Trip For Meal
A. Trip For Meal time limit per test 1 second memory limit per test 512 megabytes input standard inp ...
- [Manthan, Codefest 18][Codeforces 1037E. Trips]
题目链接:1037E - Trips 题目大意:有n个人,m天,每天晚上都会有一次聚会,一个人会参加一场聚会当且仅当聚会里有至少k个人是他的朋友.每天早上都会有一对人成为好朋友,问每天晚上最多能有多少 ...
- Codeforces 1037E Trips
原题 题目大意: 有\(n\)个人,起初他们都不是朋友.总共有\(m\)天,每天会有两个人成为朋友.他们计划在晚上出去旅游,对于一个人,有如下两种情况: 1.要么他不出去旅游 2.要么有至少\(k\) ...
- Trips CodeForces - 1037E(思维dfs)
题意: 就是几个人去旅游,组队的条件是对于某个队员 队里至少有两个是他的朋友,每天早晨都会有一对新人成为朋友 解析: 用set标记互为朋友 a[i] b[i] 表示在第i天早晨 u和v成为朋友 先求最 ...
- codeforces 1037E. Trips(倒叙)
题目传送门: 解题思路: 正着搞好像有点恶心. 反着搞. 一边删一边搞,从崩坏的地方开始,入度--. 最后dfs崩坏,更新答案. 注意要把边删掉防止重复崩坏. 代码: #include<cstd ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...
- Codeforces 703B. Mishka and trip 模拟
B. Mishka and trip time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
地址:http://codeforces.com/contest/766/problem/E 题目: E. Mahmoud and a xor trip time limit per test 2 s ...
- Codeforces Round #365 (Div. 2) Mishka and trip
Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...
随机推荐
- docker push
一.确保docker hub上有账号 二.确认要提交的镜像的命名空间为自己账号名称 三.在本地登录docker: docker login 四.提交镜像: docker push zhengchuzh ...
- Django之初
Django之初 Django的开始: #安装Django: pip3 install django #创建Django项目: django-admin startproject 项目名 #比如: d ...
- Ubuntu系统搭建django+nginx+uwsgi
1. 在开发机上的准备工作 2. 在服务器上的准备工作 3.安装uwsgi 4.编写uwsgi配置文件,使用配置文件启动uwsgi 5. 安装nginx 6. 收集静态文件 7. 编写nginx配置文 ...
- Python中接收用户的输入
一.如何去接收用户的输入?使用函数 input() 函数 input() 让程序暂停运行,等待用户输入一些文本,获取用户的输入后,Python将其存储到一个变量中,以方便后期使用. name = in ...
- python 调用 C 动态库
首先是 C 的头文件和源文件, #ifndef POINT_H #define POINT_H struct point { int x; int y; }; void point_print(str ...
- os系统下安装Python2和Python3
一.下载Xcode工具 1.在App Store 里面下载并安装Xcode 2.安装好Xcode后就打开它,首次进入会有一些LicenceAgreement,点同意就是了,然后就进入到 这个界面: 3 ...
- bzoj1052 [HAOI2007]覆盖问题 - 贪心
Description 某人在山上种了N棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他决定用3个L*L的正方形塑料薄膜将小 ...
- 【BZOJ2440】完全平方数(莫比乌斯函数,容斥原理)
题意:求第k个无平方因子数 k<=10^9 思路: 感觉这东西和欧拉筛差不多……活到老学到老,退役前学点新知识也是好的 为什么二分答案的上界是2*n?连LYY都证不出来 话说约大爷一年之前就已经 ...
- 【http_load】http_load性能测试工具使用详解
1.什么是http_loadhttp_load是一款基于Linux平台的web服务器性能测试工具,用于测试web服务器的吞吐量与负载,web页面的性能. 2.http_load的安装1)下载地址wge ...
- [运维]ELK实现日志监控告警
https://blog.csdn.net/yeweiouyang/article/details/54948846