Codeforces Round #346 (Div. 2) E. New Reform
2016-04-01 14:21
295 查看
E. New Reform
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Berland has n cities connected by m bidirectional
roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing
roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads
into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
Input
The first line of the input contains two positive integers, n and m —
the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).
Next m lines contain the descriptions of the roads: the i-th
road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi),
wherexi and yi are
the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
Output
Print a single integer — the minimum number of separated cities after the reform.
Examples
input
output
input
output
input
output
Note
In the first sample the following road orientation is allowed:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/3b/14/3b145c80265496c32b79a686aff12a38adc43bd0.png)
,
![](http://codeforces.com/predownloaded/30/21/3021ae6fa6f7c94e1f40d6b540d0929167ebc0e4.png)
.
The second sample:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/11/40/1140548295e0c0bf64e12e6f56488dfe92f20fd4.png)
,
![](http://codeforces.com/predownloaded/87/b9/87b9bfe02d6490736188a0be16d7fef0a4279045.png)
,
![](http://codeforces.com/predownloaded/5d/37/5d370bbbb6427ffa33d37f23262c8c7634bc6a33.png)
,
![](http://codeforces.com/predownloaded/30/21/3021ae6fa6f7c94e1f40d6b540d0929167ebc0e4.png)
.
The third sample:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/87/b9/87b9bfe02d6490736188a0be16d7fef0a4279045.png)
,
![](http://codeforces.com/predownloaded/0f/9b/0f9b942b5c5fa38f535dcaefa3365f4f534dc92b.png)
,
![](http://codeforces.com/predownloaded/dd/30/dd30cf6ae26f11b94fbe9e9c21e194e8edac7ef3.png)
,
![](http://codeforces.com/predownloaded/8e/6f/8e6f0f48157ac5c88e9171a93d93e383313c7177.png)
.
题意:给你n个点,m条边,(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).刚开始每条边都是无向的,然后把所有的边都改成有向,问最少有多少个点入度为0,
出度不为0的。
思路:如果一个联通图里含有边双连通,那么这个联通图的入度为0,出度不为0的点最少为0个,
如果一个联通图中不含边双连通分量,那么这个联通图的入度为0,出度不为0的点最少为1个。
#include<bits/stdc++.h>
using namespace std;
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
const int maxn=100100;
struct Edge{
int from,next,to;
}e[maxn*2];
int tot,head[maxn];
int Index,top,DFN[maxn],Low[maxn],Stack[maxn],Belong[maxn],num[maxn];
int block;
bool Instack[maxn];
void init(){
tot=0;
mem1(head);
}
void addedge(int from,int to){
e[tot].from=from;
e[tot].to=to;
e[tot].next=head[from];
head[from]=tot++;
}
void Tarjan(int u,int pre){
int v;
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
int pre_cnt=0;
for(int i=head[u];i!=-1;i=e[i].next){
v=e[i].to;
if(v==pre&&pre_cnt==0){
pre_cnt++;
continue;
}
if(!DFN[v]){
Tarjan(v,u);
if(Low[u]>Low[v])
Low[u]=Low[v];
}
else if(Instack[v]&&Low[u]>DFN[v])
Low[u]=DFN[v];
}
if(Low[u]==DFN[u]){
block++;
do{
v=Stack[--top];
num[block]++;
Instack[v]=false;
Belong[v]=block;
}while(v!=u);
}
}
int vis[maxn];
void bfs(int st){
queue<int>Q;
if(st==0){
for(int i=1;i<=block;i++)
if(num[i]>1&&vis[i]==0){
Q.push(i);
vis[i]=1;
}
}
else
Q.push(st),vis[st]=1;
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v]==1)
continue;
vis[v]=1;
Q.push(v);
}
}
}
void solve(int n){
mem0(DFN);
mem0(vis);
mem0(num);
memset(Instack,false,sizeof(Instack));
Index=top=block=0;
for(int i=1;i<=n;i++)
if(!DFN[i])
Tarjan(i,0);
int tot1=tot;
init();
for(int i=0;i<tot1;i++){
int u=Belong[e[i].from],v=Belong[e[i].to];
addedge(u,v);
}
mem0(vis);
bfs(0);
int ans=0;
for(int i=1;i<=block;i++)
if(vis[i]==0){
ans++;
bfs(i);
}
printf("%d\n",ans);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
int u,v;
init();
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n);
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Berland has n cities connected by m bidirectional
roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing
roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads
into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
Input
The first line of the input contains two positive integers, n and m —
the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).
Next m lines contain the descriptions of the roads: the i-th
road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi),
wherexi and yi are
the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
Output
Print a single integer — the minimum number of separated cities after the reform.
Examples
input
4 3 2 1 1 3 4 3
output
1
input
5 5
2 11 3
2 3
2 5
4 3
output
0
input
6 5 1 2 2 3 4 5 4 6 5 6
output
1
Note
In the first sample the following road orientation is allowed:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/3b/14/3b145c80265496c32b79a686aff12a38adc43bd0.png)
,
![](http://codeforces.com/predownloaded/30/21/3021ae6fa6f7c94e1f40d6b540d0929167ebc0e4.png)
.
The second sample:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/11/40/1140548295e0c0bf64e12e6f56488dfe92f20fd4.png)
,
![](http://codeforces.com/predownloaded/87/b9/87b9bfe02d6490736188a0be16d7fef0a4279045.png)
,
![](http://codeforces.com/predownloaded/5d/37/5d370bbbb6427ffa33d37f23262c8c7634bc6a33.png)
,
![](http://codeforces.com/predownloaded/30/21/3021ae6fa6f7c94e1f40d6b540d0929167ebc0e4.png)
.
The third sample:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/87/b9/87b9bfe02d6490736188a0be16d7fef0a4279045.png)
,
![](http://codeforces.com/predownloaded/0f/9b/0f9b942b5c5fa38f535dcaefa3365f4f534dc92b.png)
,
![](http://codeforces.com/predownloaded/dd/30/dd30cf6ae26f11b94fbe9e9c21e194e8edac7ef3.png)
,
![](http://codeforces.com/predownloaded/8e/6f/8e6f0f48157ac5c88e9171a93d93e383313c7177.png)
.
题意:给你n个点,m条边,(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).刚开始每条边都是无向的,然后把所有的边都改成有向,问最少有多少个点入度为0,
出度不为0的。
思路:如果一个联通图里含有边双连通,那么这个联通图的入度为0,出度不为0的点最少为0个,
如果一个联通图中不含边双连通分量,那么这个联通图的入度为0,出度不为0的点最少为1个。
#include<bits/stdc++.h>
using namespace std;
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
const int maxn=100100;
struct Edge{
int from,next,to;
}e[maxn*2];
int tot,head[maxn];
int Index,top,DFN[maxn],Low[maxn],Stack[maxn],Belong[maxn],num[maxn];
int block;
bool Instack[maxn];
void init(){
tot=0;
mem1(head);
}
void addedge(int from,int to){
e[tot].from=from;
e[tot].to=to;
e[tot].next=head[from];
head[from]=tot++;
}
void Tarjan(int u,int pre){
int v;
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
int pre_cnt=0;
for(int i=head[u];i!=-1;i=e[i].next){
v=e[i].to;
if(v==pre&&pre_cnt==0){
pre_cnt++;
continue;
}
if(!DFN[v]){
Tarjan(v,u);
if(Low[u]>Low[v])
Low[u]=Low[v];
}
else if(Instack[v]&&Low[u]>DFN[v])
Low[u]=DFN[v];
}
if(Low[u]==DFN[u]){
block++;
do{
v=Stack[--top];
num[block]++;
Instack[v]=false;
Belong[v]=block;
}while(v!=u);
}
}
int vis[maxn];
void bfs(int st){
queue<int>Q;
if(st==0){
for(int i=1;i<=block;i++)
if(num[i]>1&&vis[i]==0){
Q.push(i);
vis[i]=1;
}
}
else
Q.push(st),vis[st]=1;
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v]==1)
continue;
vis[v]=1;
Q.push(v);
}
}
}
void solve(int n){
mem0(DFN);
mem0(vis);
mem0(num);
memset(Instack,false,sizeof(Instack));
Index=top=block=0;
for(int i=1;i<=n;i++)
if(!DFN[i])
Tarjan(i,0);
int tot1=tot;
init();
for(int i=0;i<tot1;i++){
int u=Belong[e[i].from],v=Belong[e[i].to];
addedge(u,v);
}
mem0(vis);
bfs(0);
int ans=0;
for(int i=1;i<=block;i++)
if(vis[i]==0){
ans++;
bfs(i);
}
printf("%d\n",ans);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
int u,v;
init();
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n);
return 0;
}
相关文章推荐
- Linux C高级编程——时间编程
- H5在App中的应用
- jq 异步提交文件
- Android中的webview控件支持缩放并隐藏右下角缩放控制条
- mac和window下的html开发工具
- 给考研计划报考“管理学科学与project”方向大学生的建议(大二阶段)
- .net 安装Swagger
- Unity FisheyeShader using Spherical Mapping
- 计时器,主要讲了setInterval()和Date()方法
- 四个变量比较大小
- c web service
- 多线程之传统多线程
- linux下查看网卡信息的命令
- Eclipse安装Maven插件
- java和c语言的switch语句支持哪些数据类型
- HTML5<meta name="viewport"/>标签常见属性及说明
- CF 603A. Alternative Thinking
- 多线程之传统多线程
- 多线程之传统多线程
- Canny边缘检测算法原理及其VC实现详解(一)