您的位置:首页 > 其它

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
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: 





.

The second sample: 









.

The third sample: 









.

题意:给你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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: