软件世界网 购物 网址 三丰软件 | 小说 美女秀 图库大全 游戏 笑话 | 下载 开发知识库 新闻 开发 图片素材
多播视频美女直播
↓电视,电影,美女直播,迅雷资源↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
移动开发 架构设计 编程语言 Web前端 互联网
开发杂谈 系统运维 研发管理 数据库 云计算 Android开发资料
  软件世界网 -> 编程语言 -> CodeforcesRound#346(Div.2)(E)dfs -> 正文阅读
编程语言 最新文章
Java面试题(1)
ReactiveX序列——RxSwift
C++STL之ACM相关知识大全
c++中vector向量几种情况的总结(向量指针,
SSH框架整合demo
JAX
UVA
curl备忘(1)
C#机房重构——万事开头难(二)
OJ刷题

[编程语言]CodeforcesRound#346(Div.2)(E)dfs

  2016-04-02 20:59:08

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?0001?≤?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?≤?nxi?≠?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 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,否则至少有一个入度为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[N];
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;
}

上一篇文章      下一篇文章      查看所有文章
2016-04-02 20:55:35  
360图书馆 论文大全 母婴/育儿 软件开发资料 网页快照 文字转语音 购物精选 软件 美食菜谱 新闻中心 电影下载 小游戏 Chinese Culture
生肖星座解梦 三沣玩客 拍拍 视频 开发 Android开发 站长 古典小说 网文精选 搜图网 天下美图 中国文化英文 多播视频 装修知识库
2017-1-18 20:00:41
多播视频美女直播
↓电视,电影,美女直播,迅雷资源↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  软件世界网 --