Codeforces Round #346 (Div. 2) E. New Reform dfs
E. New Reform
题目连接:
http://www.codeforces.com/contest/659/problem/E
Description
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.
Sample Input
4 3
2 1
1 3
4 3
Sample Output
1
Hint
题意
给你一个图,现在要你给这个图里面的边定方向,使得入度为0的点最少。
题解:
对于一个连通块而言,如果里面存在一个环,那么必然所有点的入度都可以大于等于1
否则的话,就存在一个点的入度为0。
dfs一波就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
vector<int> E[maxn];
int n,m,flag;
int vis[maxn];
void dfs(int x,int fa)
{
if(vis[x])
{
flag = 1;
return;
}
vis[x]=1;
for(int i=0;i<E[x].size();i++)
{
int v=E[x][i];
if(v==fa)continue;
dfs(v,x);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
int ans = 0;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
flag = 0;
dfs(i,-1);
if(!flag)ans++;
}
}
cout<<ans<<endl;
}
Codeforces Round #346 (Div. 2) E. New Reform dfs的更多相关文章
- Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)
E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #346 (Div. 2) E - New Reform 无相图求环
题目链接: 题目 E. New Reform time limit per test 1 second memory limit per test 256 megabytes inputstandar ...
- Codeforces Round #346 (Div. 2) E. New Reform
E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)
Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...
- Codeforces Round #346 (Div. 2) A Round-House
A. Round House 题目链接http://codeforces.com/contest/659/problem/A Description Vasya lives in a round bu ...
- Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径
E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...
- Codeforces Round #346 (Div. 2) A. Round House 水题
A. Round House 题目连接: http://www.codeforces.com/contest/659/problem/A Description Vasya lives in a ro ...
- Codeforces Round #346 (Div. 2)
前三题水 A #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; int main() { int ...
- Codeforces Round #346 (Div. 2) D Bicycle Race
D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...
随机推荐
- 商城项目(ssm+dubbo+nginx+mysql统合项目)总结(3)
我不会在这里贴代码和详细步骤什么的,我觉得就算我把它贴出来,你们照着步骤做还是会出很多问题,我推荐你们去看一下黑马的这个视频,我个人感觉很不错,一步一步走下来可以学到很多东西.另外,视频和相关文档的话 ...
- Linux CGI编程基础【整理】
Linux CGI编程基础 1.为什么使用CGI? 如前面所见,任何的HTML均是静态网页,它无法实现一些复杂的功能,而CGI可以为我们实现.如:a.列出服务器上某个目录中的文件,对目录中的文件进行操 ...
- spring web 生命周期理解
spring web /bean 生命周期 反射注解 aop代理类生成 init servlet 初始化 load spring-context.xml load XmlParser 类解析对象 ...
- JVM内存分配及GC简述
在阐述JVM的内存区域之前,先来看下计算机的存储单位.从小到大依次为Bit,Byte,KB,MB,GB,TB.相邻的单位相差2的10次方. 计算机运行中的存储元件主要分为寄存器(位于CPU)和内存,寄 ...
- xpath简单应用
相对路径与绝对路径: 如果"/"处在XPath表达式开头则表示文档根元素,(表达式中间作为分隔符用以分割每一个步进表达式)如:/messages/message/subject是一 ...
- 字符串匹配的KMP算法(如何实现还需静下心来细看)
第一部分:KMP算法的理解(转:http://kb.cnblogs.com/page/176818/) 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB AB ...
- centos使用boost过程
1. 安装gcc,g++,make等开发环境 yum groupinstall "Development Tools" 2. 安装boost yum install boost ...
- Redis实战(七)
修改数据 C#语言修改Redis示例. 1.通过key修改单个value using (var redisClient = RedisManager.GetClient()) { var user = ...
- HTML5 Video/Audio播放本地文件
这段时间经常看到开发者在反复询问同一个问题,为什么通过设置src属性,不能播放本地的媒体文件?例如video.src=”D:\test.mp4”. 这是因为浏览器中的JavaScript不能直接直接访 ...
- NHibernate示例
1. 下载相关资源: 下载NHibernate.下载地址: http://nhforge.org/Default.aspx 下载微软Northwind示例数据库,下载地址:http://www.mic ...