Codeforces 659E New Reform 【DFS 求环】

来源:互联网 发布:阿里云客服考试题库 编辑:程序博客网 时间:2024/05/16 10:11

题目链接:Codeforces 659E New Reform

E. New Reform
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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), 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的点的个数变为0的。那么求环就可以了。

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#define PI acos(-1.0)#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se second#define ll o<<1#define rr o<<1|1using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 1e5 + 10;const int pN = 1e6;// <= 10^7const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;void add(LL &x, LL y) { x += y; x %= MOD; }vector<int> G[MAXN];bool vis[MAXN], flag;void DFS(int u, int root, int fa) {    //cout << u << endl;    if(vis[u]) {        flag = true;        return ;    }    vis[u] = true;    for(int i = 0; i < G[u].size(); i++) {        int v = G[u][i];        if(v == fa) continue;        DFS(v, root, u);    }}int main(){    int n, m; scanf("%d%d", &n, &m);    for(int i = 1; i <= n; i++) {        vis[i] = false;        G[i].clear();    }    for(int i = 0; i < m; i++) {        int u, v; scanf("%d%d", &u, &v);        G[u].push_back(v);        G[v].push_back(u);    }    int ans = 0;    for(int i = 1; i <= n; i++) {        if(vis[i]) continue;        flag = false; DFS(i, i, -1);        ans += flag == false;    }    printf("%d\n", ans);    return 0;}
0 0
原创粉丝点击