[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个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...
随机推荐
- 19Spring返回通知&异常通知&环绕通知
在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...
- (十三)python 3 集合
定义: 1.不同元素组成 2.无序 3.集合中的元素必须是不可变类型 创建集合 s = {1,2,3,4,5,6,7,8} 1.定义可变集合 >>> set_test = set(' ...
- 【转】Asp.net MVC Comet推送
原文链接:http://www.cnblogs.com/kissdodog/p/4283485.html 一.简介 在Asp.net MVC实现的Comet推送的原理很简单. 服务器端:接收到服务器发 ...
- linux & command line & console & logs
linux & command line & console & logs how to get the logs form linux command console htt ...
- noip模拟赛 仓库
分析:非常像货车运输那道题.先求一下最大生成树.求完之后会发现并不好处理.通常这类求生成树的题目不会就分析kruscal算法的性质.每往最大生成树中加一条边,如果配重大于这条边权,那么这条边所连的两个 ...
- 家的范围 Home on the Range(洛谷 2733)
题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因为一些原因,他的奶牛只在正方形的牧场上吃草.)遗憾的是,他的奶牛已经毁坏一些土地.( 一 ...
- POJ 3415 (后缀自动机)
POJ 3415 Common Substrings Problem : 给两个串S.T (len <= 10^5), 询问两个串有多少个长度大于等于k的子串(位置不同也算). Solution ...
- runOnUiThread在子进程中更新主进程UI
package com.pingyijinren.test; import android.support.v7.app.AppCompatActivity; import android.os.Bu ...
- docker容器-快速部署Jenkins
1.在本地虚拟机环境.安装CentOS 7,并安装docker容器 2.在docker容器中执行 docker pull jenkinsci/blueocean 3.查看已经下载的Jenkins镜像 ...
- mkswap,swapon, swapoff命令:创建交换分区
linux支持虚拟内存,用作虚拟内存的硬盘部分被称为交互空间(swap space),虚拟内存是指使用磁盘当作内存的扩展,这样可用内存的大小就相应的增大了.内核会将暂时不用的内存块的内容写到硬盘上,从 ...