Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)
Codeforces Round #346 (Div. 2)---E. New Reform
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:
,
,
,
,
.
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=+;
int par[maxn];
bool cc[maxn]; void init(int n)
{
for(int i=;i<=n;i++)
par[i]=i;
}
int find_(int x)
{
if(x==par[x])
return x;
else
return par[x]=find_(par[x]);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init(n);
memset(cc,,sizeof(cc));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
u=find_(u);
v=find_(v);
if(u==v)
{
cc[u]=;
}
else
{
par[u]=v;
cc[v]=cc[u]||cc[v];
}
}
int ans=;
for(int i=;i<=n;i++)
{
if(par[i]==i&&!cc[i])
ans++;
}
cout<<ans<<endl;
}
return ;
}
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 limit per test 256 megabytes input standard input ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集
D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...
- 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(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 #600 (Div. 2) D题【并查集+思维】
题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题
E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- 第一章:Symfony2和HTTP基本原理
恭喜你!通过学习Symfony2,你将用你自己的方式开发出更加高效.全面和流行的Web应用(当然,要受到用人单位或同行的欢迎,还是得靠你自己).Symfony2的存在是为了要解决最根本的问题:即提供一 ...
- 让VS2010/VS2012添加新类时自动添加public关键字
在VS添加类别的时候,每次都需要添加public关键字,表示好麻烦. 但是可以避免这个麻烦的. 通过修改VS2010的ItemTemplate,可以避免这个麻烦. 修改方法如下: 1. 打开文件夹Mi ...
- JQM[jquery mobile] 实战经验汇总
动态装载的子页面(data-role=”page”),完全不用page div之外的tag,也不会起作用.子页面的javascript脚本必须写在page的</div>之前. 切换按钮(a ...
- “远程调试监视器(MSVSMON.EXE)似乎没有在远程计算机上运行“的完美解决方案
今天调试程序时,Visual Studio突然报出了如下错误: Microsoft Visual Studio 远程调试监视器(MSVSMON.EXE)似乎没有在远程计算机上运行.这可能是因为防火墙阻 ...
- Flash Media Server 4.5 序列号 (fms4.5 激活码)
激活码一枚 ,网上找不到的..我今天放出来了哦... 1462-5864-7783-6034-8316-3718 (亲测 可用) 安装前找到系统盘下windows/system32/driv ...
- Nginx模块开发时unknown directive "echo"的处理
实际上,Nginx并没有echo这个指令,所以你贸然使用时,自然会提示说无法识别的指令,处理方法有两个: 方法一是: 从下面连接下载echo-nginx-module模块并安装: https://gi ...
- xming + putty 搭建远程图形化ssh访问ubuntu 14.04
putty下载: http://www.putty.org/ 一般我们远程登录linux 服务器,都是使用非加密的 telnet 或者加密的 ssh.这些登录方式有一个特点:只能登录字符界面,不能运行 ...
- WEB安全入门(转)
一. 首先你得了解Web Web分为好几层,一图胜千言:事实是这样的:如果你不了解这些研究对象是不可能搞好安全研究的.这样看来,Web有八层(如果把浏览器也算进去,就九层啦,九阳神功……)!!!每层都 ...
- js获取客户端计算机硬件及系统信息
注意:(1):遇到“automation服务器不能创建对象”的问题 解决方案: 1.在“运行”中执行regsvr32 scrrun.dll 2.安全模式设置成“中”,如果javascript脚本中报这 ...
- NopCommerce 开源商城下载配置安装
NopCommerce 是一款开源商城系统,提供了完整的解决方案.应该来说是目前最完善,功能最强劲的商城系统.基于ASP.NET MVC 框架,EF框架,数据库MSSQL. 系统版本涵盖了从MVC3. ...