2016NEFU集训第n+3场 E - New Reform
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
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
Hint
In the first sample the following road orientation is allowed: ,
,
.
The second sample: ,
,
,
,
.
The third sample: ,
,
,
,
.
//这是一道并查集判环问题,之前做过这种类似的,但是忘了,一会巩固一下;
/*总结 *****并查集判环*****
并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示。
如何用并查集来判断一个图是否有环?
此时的1同时是 2 3 的上级,如果接下来mix (2,3)
必然会连成一个环
在程序中为 if(fx==fy) a[fx]=a[fy]=a[x]=a[y]=true;//即确定1 2 3 是环的一部分
接下来mix(3,4),如果3 的环一部分,(或者是4、_find(3),_find(4))必可以每个元素都可以单向指向
如果这样。。。4 ,5 就没法都做到单向指向。。
即想每个元素都可以单向指向一定要有环。。。
******o(^▽^)o******
*/
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; int pre[];
bool a[]; int _find(int x)
{
int r=pre[x];
while(r!=pre[r])
r=pre[r];
int i=x,j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
} void mix(int x,int y)
{
int fx=_find(x),fy=_find(y);
if(fx!=fy)
{
pre[fx]=fy;
if(a[fx]||a[fy]||a[x]||a[y])
a[fx]=a[fy]=a[x]=a[y]=true;
}
else
a[fx]=a[fy]=a[x]=a[y]=true;
} int main()
{
int n,m,ans;
while(cin>>n>>m)
{
ans=;
for(int i=;i<=n;i++)
pre[i]=i;
memset(a,false,sizeof(a));
for(int i=;i<=m;i++)
{
int a,b;
cin>>a>>b;
mix(a,b);
}
for(int i=;i<=n;i++)
{
if(a[_find(i)]==false&&_find(i)==i)
ans++;
}
cout<<ans<<endl;
}
return ;
}
2016NEFU集训第n+3场 E - New Reform的更多相关文章
- 2016NEFU集训第n+5场 A - Chinese Girls' Amusement
Description You must have heard that the Chinese culture is quite different from that of Europ ...
- 2016NEFU集训第n+3场 G - Tanya and Toys
Description In Berland recently a new collection of toys went on sale. This collection consists of 1 ...
- 2016NEFU集训第n+3场 D - Bicycle Race
Description Maria participates in a bicycle race. The speedway takes place on the shores of Lake Luc ...
- Contest1692 - 2019寒假集训第三十一场 UPC 11075 Problem D 小P的国际象棋
非常简单的单点修改+区间加+区间查询.我用的是最近刚学的区间修改版本树状数组. 直接维护即可,注意修改后的单点值已经不是a[i],或者b[i],要通过区间查询求单点.不然是错的. 区间修改版本树状数 ...
- nowcoder提高集训营第5场
凉 (比赛链接)[https://www.nowcoder.com/acm/contest/177#question] T1 (题目链接)[https://www.nowcoder.com/acm/c ...
- 牛客网NOIP赛前集训营 第6场 T1 最长路
[题解] 先建反向图,然后跑拓扑排序求出最长路. 将所有的点按照最长路从小到大分层,把上一层连向这一层的边按照边权为第一关键字.起点的排名为第二关键字排序. 按照这个顺序更新这一层的答案,按照这一层每 ...
- 关于ACM,关于CSU
原文地址:http://tieba.baidu.com/p/2432943599 前言: 即将进入研二,ACM的事情也渐渐远去,记忆终将模糊,但那段奋斗永远让人热血沸腾.开个贴讲讲ACM与中南的故事, ...
- NOI前训练日记
向别人学习一波,记点流水帐.17.5.29开坑. 5.29 早晨看了道据说是树状数组优化DP的题(hdu5542),然后脑补了一个复杂度500^3的meet in the middle.然后死T... ...
- 牛客网NOIP赛前集训营-提高组(第四场)游记
牛客网NOIP赛前集训营-提高组(第四场)游记 动态点分治 题目大意: \(T(t\le10000)\)组询问,求\([l,r]\)中\(k(l,r,k<2^{63})\)的非负整数次幂的数的个 ...
随机推荐
- java四种xml解析区别
1.DOM解析 dom解析是根据树形结构解析,将整个文档加载到内存中,因此对内存的要求较高,所以可以对该文档数据进行多次操作(增,删,改,查). 2.SAX解析 SAX解析是根据事件模型解析,边读取文 ...
- jvm的垃圾回收几种理解
1.引用计数器回收 给每个对象设置一个计数器,当该对象被引用时,计数器加1,当有其他变量不再引用该对象时,计数器减1.直到计数器数值为0,回收器视为他是‘垃圾’,可以被回收,当该对象被回收时,其他引用 ...
- 约瑟夫问题-Josephus--及实例说明
//---我保证所有的代码都已经通过测试---// 类似约瑟夫的问题又称为约瑟夫环.又称“丢手绢问题”. 这个问题来自于这样的一个关于著名犹太历史学家 Josephus传说: 在罗马人占领乔塔帕特后, ...
- Spring in Action --- 第一章 简介
简化java开发 基于POJO的轻量级和最小入侵性编程 通过依赖注入和面向接口实现松耦合 基于切面和管理进行声明式编程 通过切面和模板减少样板式代码 bean的生命周期 Spring对bean进行实例 ...
- Hex Workshop(16进制编辑利器) 6.7.2绿色版
软件名称: Hex Workshop 6.7.2绿色版(16进制编辑利器) 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 1.8MB 图片预览: 软件简介 ...
- 自己封装的tc
封装弹窗 html <head> <script src="jquery.js"></script> <script src=" ...
- 跑github上的Symfony项目遇到的问题2
最近学习symfony框架,下载了大量的github上的别人的项目,要想跑起别人的项目,总结了以下几个步骤: 第一, 克隆一份代码; 第二,安装依赖,前提是安装了composer, 1:在你下载的项目 ...
- 免费DDOS攻击测试工具大合集
FreeBuf微科普: DoS(Denial Of Service)攻击是指故意的攻击网络协议实现的缺陷或直接通过野蛮手段残忍地耗尽被攻击对象的资源,目的是让目标计算机或网络无法提供正常的服务或资源访 ...
- JAVA的if用法,比如if(...){} 和if()没有大括号直接写下面的区别是什么
有大括号的时候 大括号里面所有的 都归if管.只有条件为真的时候 才会执行.没有大括号的时候 只有下面的一句归if管,也就是说 当只有一句的时候 大括号可以省略 其它的 没区别.
- Learning from the CakePHP source code - Part II
原文:http://debuggable.com/posts/learning-from-the-cakephp-source-code-part-ii:480f4dd6-57fc-4715-8709 ...