Codeforces 659E New Reform【思维+Dfs】

来源:互联网 发布:java写游戏代码 编辑:程序博客网 时间:2024/04/30 05:28

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 isnot 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 consideredseparate, 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: thei-th road is determined by two distinct integersxi, yi (1 ≤ xi, yi ≤ n,xi ≠ yi), wherexi andyi are the numbers of the cities connected by thei-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 32 11 34 3
Output
1
Input
5 52 11 32 32 54 3
Output
0
Input
6 51 22 34 54 65 6
Output
1
Note

In the first sample the following road orientation is allowed: ,,.

The second sample: ,,,,.

The third sample: ,,,,.


题目大意:

给你N个点,M条无向边(没有重边),让你将这M条无向边规定方向变成有向边,使得最终的有向图中,入度为0的点最少,问最少有多少个。


思路:


对于一个联通块而言,如果其中有环存在,那么对应就一定要把这个环规定为强连通分量(并且形状就是一个环)。那么接下来剩余的部分我们只要从这个强连通分量中的点连出即可,使得整个连通块部分没有入度为0的部分,相反,如果一个联通块没有环存在,那么我们规定一个点作为入度为0的点,将其相关的边连出即可,这样就能保证其他的点的入度一定不为0。


那么接下来的实现我们Dfs判断这个无向图的各个连通部分是否存在环即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>using namespace std;int n,m,flag;vector<int >mp[100060];int vis[100060];void Dfs(int u,int from){    vis[u]=1;    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i];        if(v==from)continue;        if(vis[v]==1)        {            flag=1;            continue;        }        Dfs(v,u);    }}int main(){    while(~scanf("%d%d",&n,&m))    {        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)mp[i].clear();        for(int i=0;i<m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            mp[x].push_back(y);            mp[y].push_back(x);        }        int output=0;        for(int i=1;i<=n;i++)        {            if(vis[i]==0)            {                flag=0;                Dfs(i,-1);                if(flag==0)output++;            }        }        printf("%d\n",output);    }}








0 0
原创粉丝点击