Codeforces Round #346 (Div. 2) E. New Reform
2016-04-02 18:55
337 查看
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 ≤ 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
output
input
output
input
output
Note
In the first sample the following road orientation is allowed:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/3b/14/3b145c80265496c32b79a686aff12a38adc43bd0.png)
,
![](http://codeforces.com/predownloaded/30/21/3021ae6fa6f7c94e1f40d6b540d0929167ebc0e4.png)
.
The second sample:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/11/40/1140548295e0c0bf64e12e6f56488dfe92f20fd4.png)
,
![](http://codeforces.com/predownloaded/87/b9/87b9bfe02d6490736188a0be16d7fef0a4279045.png)
,
![](http://codeforces.com/predownloaded/5d/37/5d370bbbb6427ffa33d37f23262c8c7634bc6a33.png)
,
![](http://codeforces.com/predownloaded/30/21/3021ae6fa6f7c94e1f40d6b540d0929167ebc0e4.png)
.
The third sample:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/87/b9/87b9bfe02d6490736188a0be16d7fef0a4279045.png)
,
![](http://codeforces.com/predownloaded/0f/9b/0f9b942b5c5fa38f535dcaefa3365f4f534dc92b.png)
,
![](http://codeforces.com/predownloaded/dd/30/dd30cf6ae26f11b94fbe9e9c21e194e8edac7ef3.png)
,
![](http://codeforces.com/predownloaded/8e/6f/8e6f0f48157ac5c88e9171a93d93e383313c7177.png)
.
一开始觉得 是联通分量的个数-1 后来画图发现,每个联通分量如果存在环,那么他就没有 separate点。
一开始用并查集做,没调出来.....好弱..
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 ≤ 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:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/3b/14/3b145c80265496c32b79a686aff12a38adc43bd0.png)
,
![](http://codeforces.com/predownloaded/30/21/3021ae6fa6f7c94e1f40d6b540d0929167ebc0e4.png)
.
The second sample:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/11/40/1140548295e0c0bf64e12e6f56488dfe92f20fd4.png)
,
![](http://codeforces.com/predownloaded/87/b9/87b9bfe02d6490736188a0be16d7fef0a4279045.png)
,
![](http://codeforces.com/predownloaded/5d/37/5d370bbbb6427ffa33d37f23262c8c7634bc6a33.png)
,
![](http://codeforces.com/predownloaded/30/21/3021ae6fa6f7c94e1f40d6b540d0929167ebc0e4.png)
.
The third sample:
![](http://codeforces.com/predownloaded/ab/3a/ab3aa0026cb5ab74ad3902d3590f907b613f69c4.png)
,
![](http://codeforces.com/predownloaded/87/b9/87b9bfe02d6490736188a0be16d7fef0a4279045.png)
,
![](http://codeforces.com/predownloaded/0f/9b/0f9b942b5c5fa38f535dcaefa3365f4f534dc92b.png)
,
![](http://codeforces.com/predownloaded/dd/30/dd30cf6ae26f11b94fbe9e9c21e194e8edac7ef3.png)
,
![](http://codeforces.com/predownloaded/8e/6f/8e6f0f48157ac5c88e9171a93d93e383313c7177.png)
.
一开始觉得 是联通分量的个数-1 后来画图发现,每个联通分量如果存在环,那么他就没有 separate点。
一开始用并查集做,没调出来.....好弱..
/* *********************************************** Author : Created Time :2016/4/2 16:53:35 File Name :346e.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 100010 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; priority_queue<int,vector<int>,greater<int> >pq; struct Node{ int x,y; }; struct cmp{ bool operator()(Node a,Node b){ if(a.x==b.x) return a.y> b.y; return a.x>b.x; } }; bool cmp(int a,int b){ return a>b; } int vis[maxn]; int n,m; vector<int>v[maxn]; int mark=0; void dfs(int u,int fa){ if(vis[u]){ mark=1;return; } vis[u]=1; for(int i=0;i<v[u].size();i++){ int V=v[u][i]; if(V==fa)continue; dfs(V,u); } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int x,y; while(cin>>n>>m){ cle(vis);mark=0; for(int i=1;i<=m;i++){ scanf("%d %d",&x,&y); v[x].push_back(y); v[y].push_back(x); } int ans=0; for(int i=1;i<=n;i++){ mark=0; if(!vis[i]){ dfs(i,-1); if(mark==0)ans++; } } cout<<ans<<endl; } return 0; }
相关文章推荐
- 移植mjpg-streamer
- hdu4123(树的直径+ST算法)
- UITableView的介绍及使用
- 浅析ltp测试流程
- 可视化MNIST之降维探索Visualizing MNIST: An Exploration of Dimensionality Reduction-2
- IntervalZero RTX 2014
- UVa 1339 - Ancient Cipher
- 全版本Genymotion模拟器创建过程所需的.ova文件下载地址说明
- 面向过程到面向对象-入门,心得(实例剖析)
- POJ 1789 Truck History
- POJ 1789 Truck History 最小生成树
- 感悟
- Java高级部分之线程重点总结(下)
- April Fools Day Contest 2016 B. Scrambled
- hdu 5170 精度控制
- C++虚函数表小记
- MVVM下RadioButton单选钮和ComboBox下拉框的使用
- 【BZOJ-2223】PATULJCI 可持久化线段树
- Codeforces Round #346 (Div. 2) D. Bicycle Race
- mini2440基于boa服务器通过WEB控制GPIO