Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)
1 second
256 megabytes
standard input
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.
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.
Print a single integer — the minimum number of separated cities after the reform.
4 3
2 1
1 3
4 3
1
5 5
2 1
1 3
2 3
2 5
4 3
0
6 5
1 2
2 3
4 5
4 6
5 6
1
In the first sample the following road orientation is allowed: , , .
The second sample: , , , , .
The third sample: , , , , .
题意:n个城市,m条双向道路,现在将双向改成单向,求入度 为0的最少有几个
如果是环的话就是0,如果以x点出发深搜 发现能搜到的点已经访问过此事 x 点的入读可以不是0了,因为从那个访问过的点出发能访问到x,如果以x出发所以的点都没访问过,那么x的入读为0以x作为原点来指向那些点们
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int Max = + ;
struct Edge
{
int to;
int Next;
};
Edge edge[Max * ];
int n, m;
int tot, head[Max], vis[Max];
bool hasVisit(int u, int fa)
{
vis[u] = ;
bool flag = false;
for (int i = head[u]; i != -; i = edge[i].Next)
{
int v = edge[i].to;
if (fa == v)
continue;
if (vis[v])
{
flag = true;
continue;
}
if (hasVisit(v, u))
flag = true;
}
return flag;
}
void addEdge(int u, int v)
{
edge[tot].to = v;
edge[tot].Next = head[u];
head[u] = tot++; edge[tot].to = u;
edge[tot].Next = head[v];
head[v] = tot++;
}
int main()
{
scanf("%d%d", &n, &m);
int u, v;
memset(head, -, sizeof(head));
tot = ;
for (int i = ; i < m; i++)
{
scanf("%d%d", &u, &v);
addEdge(u, v);
}
int ans = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= n; i++)
{
if (vis[i])
continue;
if (!hasVisit(i, -))
ans++;
}
//cout << ans << endl;
printf("%d\n", ans);
return ;
}
/* 但是如果输入
2 2
1 2
2 1
按说 1 2 和 2 1是一样的但是结果却是 0
*/
Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)的更多相关文章
- 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 ...
- 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 #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #298 (Div. 2) A、B、C题
题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...
- 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 ...
随机推荐
- JVM内存管理------GC算法精解(五分钟教你终极算法---分代搜集算法)
引言 何为终极算法? 其实就是现在的JVM采用的算法,并非真正的终极.说不定若干年以后,还会有新的终极算法,而且几乎是一定会有,因为LZ相信高人们的能力. 那么分代搜集算法是怎么处理GC的呢? 对象分 ...
- html 文本超过显示省略号
display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;//显示行数 word-break: break-a ...
- yii2权限控制rbac之详细操作步骤
本篇的主题是 rbac权限控制的详细操作步骤,注意是操作步骤哦,关于配置与rbac的搭建,我们在博文 yii2搭建完美后台并实现rbac权限控制实例教程说的再清楚不过了. 但是,在很多人的反馈下,说是 ...
- 总有一天会NB的! SB一样的坚持会有NB一样的结果的!
第一天: 1.背景图不显示,因为背景图片无法撑开div,所以必须自己设置div的高度哦! 2.div水平居中! A:margin-left:auto;margin-right:auto; B: 父元 ...
- [转]easyui 全部图标
原文地址:http://blog.163.com/shexinyang@126/blog/static/136739312201491011492263/ 拿jquery-easyui-1.2.6举例 ...
- 内存溢出OOM与内存泄漏ML
附, 微信团队原创分享:Android内存泄漏监控和优化技巧总结 一.如何避免OOM 异常 想要避免OOM 异常首先我们要知道什么情况下会导致OOM 异常. 1.图片过大导致OOM Android 中 ...
- C#-WinForm-公共控件的基本属性及练习
视图→工具箱 基本操作:控件的取值.赋值.改值.事件 1.Button --按钮 AutoSize-指示该控件是否自动调整自身的大小以适应其内容的大小. 默认False,此时文字内容超过其宽度时自动 ...
- ASP.NET MVC html help
public static class HtmlHelper { /// <summary> /// 返回没有边框的只读的TextBox标签 /// </summary> // ...
- asp.net mvc 依赖缓存启动项配置
msdn 参考地址:https://msdn.microsoft.com/zh-cn/library/ms229862 4.5 第一步:32bit%windir%\Microsoft.NET\Fram ...
- 新版汉诺塔(UVa10795 - A Different Task)
题目介绍: 标准的汉诺塔上有n个大小各异的盘子.现给定一个初始局面(见图1),求它到目标局面(见图2)至少需要移动多少步? 移动规则:一次只能移动一个盘子:且在移动盘子之前,必须把压在上面的其他盘子先 ...