Codeforces Round #346 (Div. 2)(E)dfs
2016-04-01 17:39
239 查看
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),
wherexi 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:
,
,
.
The second sample:
,
,
,
,
.
The third sample:
,
,
,
,
.
题意: 有n个点,m条边,开始这些边是无向边,让你确定这些边的方向,使得最后至少有多少个点入度为0
题解:没有形成环的点形成的联通图,至少有一个入度为0的点,那么就是去判断是环,如果形成环就是0,否则至少有一个入度为0的点,那么最好使用dfs判断是否有环。复杂度O(n+m)
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(1e5+10)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
int vis
;
vector< vector<int> >g;
int flag;
void dfs(int x, int fa)
{
if (vis[x])
{
flag = 1;
return;
}
vis[x] = 1;
for (int i = 0; i < g[x].size(); i++)
{
if (g[x][i] == fa)
continue;
dfs(g[x][i], x);
}
}
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif
int n, m,x,y;
while (~scanf("%d%d", &n, &m))
{
int ans = 0;
memset(vis, 0, sizeof(vis));
g.clear();
g.resize(n + 10);
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++)
{
flag = 0;
dfs(i, -1);
ans += !flag;
}
printf("%d\n", ans);
#ifdef CDZSC
printf("time:%d\n", clock() - _time_jc);
#endif
}
return 0;
}
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),
wherexi 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:
,
,
.
The second sample:
,
,
,
,
.
The third sample:
,
,
,
,
.
题意: 有n个点,m条边,开始这些边是无向边,让你确定这些边的方向,使得最后至少有多少个点入度为0
题解:没有形成环的点形成的联通图,至少有一个入度为0的点,那么就是去判断是环,如果形成环就是0,否则至少有一个入度为0的点,那么最好使用dfs判断是否有环。复杂度O(n+m)
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(1e5+10)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
int vis
;
vector< vector<int> >g;
int flag;
void dfs(int x, int fa)
{
if (vis[x])
{
flag = 1;
return;
}
vis[x] = 1;
for (int i = 0; i < g[x].size(); i++)
{
if (g[x][i] == fa)
continue;
dfs(g[x][i], x);
}
}
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif
int n, m,x,y;
while (~scanf("%d%d", &n, &m))
{
int ans = 0;
memset(vis, 0, sizeof(vis));
g.clear();
g.resize(n + 10);
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++)
{
flag = 0;
dfs(i, -1);
ans += !flag;
}
printf("%d\n", ans);
#ifdef CDZSC
printf("time:%d\n", clock() - _time_jc);
#endif
}
return 0;
}
相关文章推荐
- web开发相关技术小结
- JAVA的ReentrantLock与synchronized 的区别
- [转] Creating a Simple RESTful Web App with Node.js, Express, and MongoDB
- MATLAB中快速删除矩阵中满足条件的行列
- 超级简单的接口回调
- 解决imagettftext()因为–enable-gd-jis-conv导致乱码的另一种方法
- mongodb安装
- 深度学习Deep Learning: dropout策略防止过拟合
- android shape的使用
- hdu 1372 Knight Moves bfs搜索 解题报告
- [svc][op]Ubuntu初始化安装-py用机器优化
- 微信支付插件使用
- Acitivity切换黑屏
- Android ActionBar应用实战,高仿微信主界面的设计
- 通过包名,直接精确启动一个三方Activity
- unexpected '@' in member unxpected '@' in program
- webclient 爬虫bug
- 找回曾经的感觉
- 详说Angular之指令(directive)
- HDU3336-Count the string(KMP)