Codeforces 659E New Reform【思维+Dfs】
2016-12-21 21:05
453 查看
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)
.
题目大意:
给你N个点,M条无向边(没有重边),让你将这M条无向边规定方向变成有向边,使得最终的有向图中,入度为0的点最少,问最少有多少个。
思路:
对于一个联通块而言,如果其中有环存在,那么对应就一定要把这个环规定为强连通分量(并且形状就是一个环)。那么接下来剩余的部分我们只要从这个强连通分量中的点连出即可,使得整个连通块部分没有入度为0的部分,相反,如果一个联通块没有环存在,那么我们规定一个点作为入度为0的点,将其相关的边连出即可,这样就能保证其他的点的入度一定不为0。
那么接下来的实现我们Dfs判断这个无向图的各个连通部分是否存在环即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int n,m,flag;
vector<int >mp[100060];
int vis[100060];
void Dfs(int u,int from)
{
vis[u]=1;
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(v==from)continue;
if(vis[v]==1)
{
flag=1;
continue;
}
Dfs(v,u);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)mp[i].clear();
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
int output=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
flag=0;
Dfs(i,-1);
if(flag==0)output++;
}
}
printf("%d\n",output);
}
}
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 11 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)
.
题目大意:
给你N个点,M条无向边(没有重边),让你将这M条无向边规定方向变成有向边,使得最终的有向图中,入度为0的点最少,问最少有多少个。
思路:
对于一个联通块而言,如果其中有环存在,那么对应就一定要把这个环规定为强连通分量(并且形状就是一个环)。那么接下来剩余的部分我们只要从这个强连通分量中的点连出即可,使得整个连通块部分没有入度为0的部分,相反,如果一个联通块没有环存在,那么我们规定一个点作为入度为0的点,将其相关的边连出即可,这样就能保证其他的点的入度一定不为0。
那么接下来的实现我们Dfs判断这个无向图的各个连通部分是否存在环即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int n,m,flag;
vector<int >mp[100060];
int vis[100060];
void Dfs(int u,int from)
{
vis[u]=1;
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(v==from)continue;
if(vis[v]==1)
{
flag=1;
continue;
}
Dfs(v,u);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)mp[i].clear();
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
int output=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
flag=0;
Dfs(i,-1);
if(flag==0)output++;
}
}
printf("%d\n",output);
}
}
相关文章推荐
- Codeforces 782C Andryusha and Colored Balloons【思维+Dfs暴力染色】
- DFS(思维) CodeForces - 525D Arthur and Walls
- Codeforces 760C Pavel and barbecue【思维+Dfs】
- Codeforces 659E New Reform 【DFS 求环】
- Codeforces 732F Tourist Reform【思维+边双联通+Dfs处理后继问题】好题!
- Codeforces 456 div2 A B ***D(思维题) ***E(二分/DFS/数论)
- codeforces-Fox And Two Dots【DFS】(思维)
- Codeforces 430C Xor-tree【Dfs+思维】
- 【CodeForces - 638】C 【思维+DFS】
- CodeForces - 841D Leha and another game about graph 构造(dfs + 思维)
- 【CodeForces 525D】【dfs+思维】 Arthur and Walls 【只包含点和星的矩阵,需要将部分星变成点使点能组成矩形 】
- Codeforces 659E New Reform【DFS】
- Codeforces 681D Gifts by the List【思维+Dfs】
- Codeforces 782E Underground Lab【极限思维+Dfs】
- Codeforces 659E New Reform【DFS】
- Codeforces 369C Valera and Elections【思维+Dfs】
- Codeforces 618D Hamiltonian Spanning Tree【思维+Dfs】
- CodeForces 659E New Reform (DFS)
- Codeforces 813C The Tag Game【思维+Dfs】
- codeforces 395 D <思维,DFS想了半天超时>