Codeforces Round #346 (Div. 2)(E)dfs

来源:互联网 发布:守望先锋数据app 编辑:程序博客网 时间:2024/05/01 13:19

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 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), 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 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条边,开始这些边是无向边,让你确定这些边的方向,使得最后至少有多少个点入度为0



题解:没有形成环的点形成的联通图,至少有一个入度为0的点,那么就是去判断是环,如果形成环就是0,否则至少有一个入度为0的点,那么最好使用dfs判断是否有环。复杂度O(n+m)




#include<cstdio>  #include<cstring>  #include<cstdlib>  #include<cmath>  #include<iostream>  #include<algorithm>  #include<vector>  #include<map>  #include<set>  #include<queue>  #include<string>  #include<bitset>  #include<utility>  #include<functional>  #include<iomanip>  #include<sstream>  #include<ctime>  using namespace std;#define N int(1e5+10)  #define inf int(0x3f3f3f3f)  #define mod int(1e9+7)  typedef long long LL;int vis[N];vector< vector<int> >g;int flag;void dfs(int x, int fa){if (vis[x]){flag = 1;return;}vis[x] = 1;for (int i = 0; i < g[x].size(); i++){if (g[x][i] == fa)continue;dfs(g[x][i], x);}}int main(){#ifdef CDZSC  freopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);  int _time_jc = clock();#endif  int n, m,x,y;while (~scanf("%d%d", &n, &m)){int ans = 0;memset(vis, 0, sizeof(vis));g.clear();g.resize(n + 10);for (int i = 0; i < m; i++){scanf("%d%d", &x, &y);g[x].push_back(y);g[y].push_back(x);}for (int i = 1; i <= n; i++){flag = 0;dfs(i, -1);ans += !flag;}printf("%d\n", ans);#ifdef CDZSCprintf("time:%d\n", clock() - _time_jc);#endif}return 0;}










0 0
原创粉丝点击