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;}
- Codeforces 659E New Reform 【DFS 求环】
- Codeforces 659E New Reform【DFS】
- CodeForces 659E New Reform (DFS)
- Codeforces 659E New Reform【思维+Dfs】
- codeforces 659 E New Reform
- codeforces 659E New Reform
- CodeForces 659E New Reform
- codeforces 659E New Reform
- codeforces 659E New Reform
- CodeForces 659E New Reform
- Codeforces--659E--New Reform(深搜好题)
- Codeforces Round #346 (Div. 2)E. New Reform 乱搞dfs
- codeforce 659E New Reform
- CodeForces 659E New Reform (图的遍历判环)
- CodeForces - 659E New Reform (网络流)求最少的入度为0的点的个数
- codefoces_#346E - New Reform(并查集或dfs)
- Codeforces Round #346 (Div. 2) E. New Reform
- Codeforces Round #346 (Div. 2) E. New Reform
- 如何解析assets目录下的xml文件,,并展示
- 详解一道C++笔试题,考察重载、覆盖、多态
- 欢迎使用CSDN-markdown编辑器
- 大数据下的数据分析-Hadoop架构解析
- 32位和64位系统区别
- Codeforces 659E New Reform 【DFS 求环】
- HTTPS原理详解
- 登录页面重定向后页面样式丢失
- Java23中设计模式一:单例模式
- 时间,日期格式化以及 格式化时间戳
- hdu4786Fibonacci Tree(最小生成树)
- javaweb项目中对于资源文件的读取(to be continue)
- JSON.parse()和JSON.stringify()
- 还有没有道德的底线