Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)

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 ≤ 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.

Examples
input
4 3
2 1
1 3
4 3
output
1
input
5 5
2 1
1 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条双向道路,现在将双向改成单向,求入度 为0的最少有几个

如果是环的话就是0,如果以x点出发深搜 发现能搜到的点已经访问过此事 x 点的入读可以不是0了,因为从那个访问过的点出发能访问到x,如果以x出发所以的点都没访问过,那么x的入读为0以x作为原点来指向那些点们

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 const int Max = 100000 + 10;
 7 struct Edge
 8 {
 9     int to;
10     int Next;
11 };
12 Edge edge[Max * 2];
13 int n, m;
14 int tot, head[Max], vis[Max];
15 bool hasVisit(int u, int fa)
16 {
17     vis[u] = 1;
18     bool flag = false;
19     for (int i = head[u]; i != -1; i = edge[i].Next)
20     {
21         int v = edge[i].to;
22         if (fa == v)
23             continue;
24         if (vis[v])
25         {
26             flag = true;
27             continue;
28         }
29         if (hasVisit(v, u))
30             flag = true;
31     }
32     return flag;
33 }
34 void addEdge(int u, int v)
35 {
36     edge[tot].to = v;
37     edge[tot].Next = head[u];
38     head[u] = tot++;
39 
40     edge[tot].to = u;
41     edge[tot].Next = head[v];
42     head[v] = tot++;
43 }
44 int main()
45 {
46     scanf("%d%d", &n, &m);
47     int u, v;
48     memset(head, -1, sizeof(head));
49     tot = 0;
50     for (int i = 0; i < m; i++)
51     {
52         scanf("%d%d", &u, &v);
53         addEdge(u, v);
54     }
55     int ans = 0;
56     memset(vis, 0, sizeof(vis));
57     for (int i = 1; i <= n; i++)
58     {
59         if (vis[i])
60             continue;
61         if (!hasVisit(i, -1))
62             ans++;
63     }
64    //cout << ans << endl;
65     printf("%d\n", ans);
66     return 0;
67 }
68 /* 但是如果输入
69 2 2
70 1 2
71 2 1
72 按说 1 2 和 2 1是一样的但是结果却是 0 
73 */
View Code

 

posted @ 2016-04-21 19:43  zhaop  阅读(321)  评论(1编辑  收藏  举报