cf246 ENew Reform (并查集找环)
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<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int fa[N];
bool flag[N];
int find(int x)
{
int r=x;
while(fa[r]!=r) r=fa[r];
int i=x,j;
while(i!=r) {
j=fa[i];
fa[i]=r;
i=j;
}
return r;
}
int main()
{
int n,m,i,j;
int x,y,fx,fy;
int ans;
ans=0;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++) fa[i]=i;
while(m--) {
scanf("%d%d",&x,&y);
fx=find(x);
fy=find(y);
if(fx!=fy) {
fa[fx]=fy;
if(flag[x]||flag[y]||flag[fx]||flag[fy])
flag[fy]=flag[fx]=flag[x]=flag[y]=true;
}
else flag[fy]=flag[fx]=flag[x]=flag[y]=true;
}
for(i=1;i<=n;i++) {
if(find(i)==i&&!flag[find(i)]) ans++;
}
printf("%d\n",ans);
return 0;
}
cf246 ENew Reform (并查集找环)的更多相关文章
- Codeforces Round #346 (Div. 2) E题 并查集找环
E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
- bzoj1116 [POI2008]CLO——并查集找环
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1116 分析性质,只要有环,那么给环定一下向就满足了条件: 环上点的其他边可以指向外面,所以两 ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- HDU - 4514 湫湫系列故事——设计风景线(并查集判环)
题目: 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n ...
- HDU 4514并查集判环+最长路
点击打开链接 题意:中文题...... 思路:先推断是否能成环,之前以为是有向图,就用了spfa推断,果断过不了自己出的例子,发现是无向图.并查集把,两个点有公共的父节点,那就是成环了,之后便是求最长 ...
- A simple problem(并查集判环)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2497 题意:给定一些点和边的关系,判断S点是否 ...
- 2019 蓝桥杯国赛 B 组模拟赛 E 蒜头图 (并查集判环)
思路: 我们看条件,发现满足条件的子图无非就是一些环构成的图, 因为只有形成环,才满足边的两个点都在子图中,并且子图中节点的度是大于0的偶数. 那么如果当前有k个环,我们可以选2^k-1个子图,为什么 ...
- LA3644简单并查集判环
题意: 有n个化合物,每个化合物是两种元素组成,现在要装车,但是一旦车上的化合物中的某几个化合物组成这样一组关系,有n个化合物正好用了n中元素,那么就会爆炸,输入的顺序是装车的顺序,对于每 ...
随机推荐
- Web前端技术体系大全搜索
一.前端技术框架 1.Vue.js 官网:https://cn.vuejs.org/ Vue CLI:https://cli.vuejs.org/ 菜鸟教程:http://www.runoob.com ...
- 字典(trie)树--从入门到入土
今天再来认识一个强大的数据结构. 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词 ...
- centos7 rsync+inotify软件实现集群服务的数据备份(一)
一.rsync软件的说明: 1.1 什么是rsync rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.它使用所谓的“Rsync演算法”来使本地和远程两个主机之间的文件达 ...
- 枚举(enum)的使用
在开发中我们经常会遇到一些特殊的字段,比如订单状态.支付状态.类型等,这些特殊字段在编码开发的时候,可以写成枚举类型.接下来还是看Demo吧! public enum AuditState { Wai ...
- (5) openssl speed(测试算法性能)和openssl rand(生成随机数)
1.1 openssl speed 测试加密算法的性能 支持的算法有: openssl speed [md2] [mdc2] [md5] [hmac] [sha1] [rmd160] [idea-cb ...
- ps---打开文件及图片保存格式
1.打开图片,可以按Ctrl或者Shift来进行多张图片的选择或者用鼠标框选. 2.勾选图像序列,可以选择命名上有次序的多个图像. 3. PSD是ps里面的标准保存格式,包含颜色.图层.通道.路径.动 ...
- 上海苹果维修点分享苹果电脑MACBOOK故障维修常见案例
苹果的电子设备无论是外观和性能都是无与伦比的美丽,很多开发者都开始选用苹果电脑macbook.近年来苹果售后维修点来维修苹果电脑的用户也越来越多,我们上海苹果维修点就整理分享了一些苹果电脑MACBOO ...
- Android开发——后台获取用户点击位置坐标(可获取用户支付宝密码)
1. getevent命令 我们首先是根据adb shell getevent命令获取到被点击位置的信息. 这里要说明的是,不同的手机手机获得的点击输出是不一样的.以我的真机为例,输出如下 本文原创, ...
- 洛谷 P1434 [SHOI2002]滑雪
这道题适合记忆化练手 毕竟总有些大佬虐题. 这个题有几个剪枝 1.记忆化 这个不用多说了吧 剪枝就是 如果 当前点到下面一个点的目前下降的高度+1 小于 下面那个点 能下降的高度 那么反过来,这个点不 ...
- OpenSSH高级功能之端口转发(Port Forwarding)
在RedHat提供的系统管理员指南中提到OpenSSH不止是一个安全shell,它还具有X11转发(X11 Forwarding)和端口转发(Port Forwarding)的功能.X11功能一般用于 ...