Codeforces--659E--New Reform(深搜好题)

来源:互联网 发布:数据分析平台交互设计 编辑:程序博客网 时间:2024/05/16 10:47
New Reform
Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Submit Status

Description

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 0001 ≤ 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 ≤ nxi ≠ yi), where xi 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.

Sample Input

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

Hint

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

The second sample: .

The third sample: .

Source

Codeforces Round #346 (Div. 2)

题意:n个点中存在m条无向边,如果在这m条边加上方向,方向唯一,但是可以是任意的,求入度为0的点的个数最多是多少

思路:可以先画个图试试,如果一个点想让自己的入度不为0,最起码要有一个点指向他,但是这样的话指向它的那一个点也需要有一个点指向自己,如果是一个环的话最好了,大家可以相互指,所以在一个图中,如果有一个环的话点的入度都不会是0,因为环上的点可以相互指,不在环上的点可以由环上的点发出的线指,

这道题并查集判环应该不可以吧,我是没想到这个方法,因为可能有多个独立子图,因为这样,我们必须判断每一个独立子图是否有一个环,但是并查集做不到,我们只能判断有没有环生成,scc吗?应该也不是。如果从一个点开始访问到达一个已经访问过的点,这样的话就可以由这个访问过的点直接或者间接的给与起点入度,可以想象一下,一个环上的一个分支的起点开始访问,然后因为环的原因,访问又会回单一个已经访问过的点,如果没有环的话那这个点就要牺牲,给其他的点入度

因为是无向边,所以访问的时候要防止出现1--2,2--1这种情况,传参的时候加上起点,每次往下访问判断这次的起点还有上次的终点是否相同

#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<algorithm>using namespace std;#define MAXN 100000+10vector<int>g[MAXN];int vis[MAXN],n,m,ans;void dfs(int pos,int p){if(vis[pos]){ans=0;return ;}vis[pos]=1;for(int i=0;i<g[pos].size();i++){if(g[pos][i]!=p)dfs(g[pos][i],pos);}}int main(){while(scanf("%d%d",&n,&m)!=EOF){memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++)g[i].clear();int x,y;for(int i=0;i<m;i++){scanf("%d%d",&x,&y);g[x].push_back(y);g[y].push_back(x);}int sum=0;for(int i=1;i<=n;i++){if(vis[i])continue;ans=1;dfs(i,0);sum+=ans;}printf("%d\n",sum);}return 0;}


0 0