Codeforces Round #346 (Div. 2) E题 并查集找环
E. New 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.
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.
Input
Output
题意:n个城市,m条双向路,将这些路改成单向的,如果一个城市没有通向它的路,(入度为0)就说明该城市是单独的。问修改后最少有几个单独的城市,要使结果最小。
思路:
1.可以建成一个有向图,可能有k个联通块,如果一个联通块没有环,就说明这个联通块,至少有一个城市单独的,因此就化成找联通块和环的问题
2.联通块的话可以用并查集来维护,然后用cir[maxn]数组来标记是否有环,如果这个联通块的根节点存在环,那么该联通块不存在单独的城市,如果不存在环的话cnt++,最后的cnt就是答案;
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define N 150000
int f[N];
int arr[N];
int getf(int v){
if(v==f[v]){
return f[v];
}
f[v]=getf(f[v]);
return f[v];
}
void merge(int u,int v){
int t1=getf(u);
int t2=getf(v);
if(t1!=t2){
f[t2]=t1;
if(arr[t2]){
arr[t1]=t2;
}
}else{
arr[t1]=;
}
}
int n,m;
void init(){
;i<=n;i++)
f[i]=i;
}
int main(){
cin>>n>>m;
init();
;
;i<=m;i++){
int x,y;
cin>>x>>y;
merge(x,y);
}
;i<=n;i++){
if(f[i]==i&&!arr[i]){
ans++;
}
}
cout<<ans;
;
}
Codeforces Round #346 (Div. 2) E题 并查集找环的更多相关文章
- 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 #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
- Codeforces Round #623 (Div. 2) D.Recommendations 并查集
ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...
- Codeforces Round #346 (Div. 2) A题 [一道让我生气的思维题·]
A. Round House Vasya lives in a round building, whose entrances are numbered sequentially by integer ...
- Codeforces Round #346 (Div. 2) C题
C. Tanya and Toys In Berland recently a new collection of toys went on sale. This collection consist ...
- Codeforces Round #346 (Div. 2) B题
B. Qualifying Contest Very soon Berland will hold a School Team Programming Olympiad. From each of t ...
随机推荐
- 使用pycharm开发web——django2.1.5(二)创建一个app并做一些配置
这里我学习的呢是刘江老师的站,主要原因在于他这个版本新,还比较细节 网址先留一手,约等于在引用http://www.liujiangblog.com/ 开始正题: 1.在pycharm界面终端命令行里 ...
- Python Excel文件的读写操作(xlwt xlrd xlsxwriter)
转:https://www.cnblogs.com/ultimateWorld/p/8309197.html Python语法简洁清晰,作为工作中常用的开发语言还是很强大的(废话). python关于 ...
- QAbstractItemModel使用样例与解析(Model::index使用了createIndex,它会被销毁吗?被销毁了,因为栈对象出了括号就会被销毁)
参考:qt源码 qstandarditemmodel_p.h qstandarditemmodel.h qstandarditemmodel.cpp qabstractitemmodel.h qabs ...
- MySQL Select语句的执行顺序
源文章:How is a query executed in MySQL? 当执行SQL的Select查询语句时,SQL指令的执行顺序如下: FROM 子句 WHERE 子句 GROUP BY 子句 ...
- C++ 构造函数后面的冒号的作用
其实冒号后的内容是初始化成员列表,一般有三种情况: 1.对含有对象成员的对象进行初始化,例如, 类line有两个私有对象成员startpoint.endpoint,line的构造函数写 ...
- MongoDB divide 使用之mongotempalte divide
需求:求一组数的两个字段计算结果的平均值 如有一组这样的数: 列名1 列名2 列名3 第一组数 a 2 5 第二组数 b 4 8 按照列名1分组 ...
- Xcode8警告⚠️ Empty paragraph passed to '@xxx' command
问题 Xcode8升级后,之前添加的注释会有很多警告 解决方法 基础知识,就是在编译选项中,添加警告屏蔽 解决步骤 显示警告信息 显示警告信息.png 查看警告类型 查看警告类型.png 屏蔽警告 W ...
- iOS AVPlayer 的使用实践
前两天在网上看到一篇博客,介绍AVPlayer的使用,但是只简单介绍了一下单个的本地文件如何播放,心血来潮,就想着做一个类似于播放器的东西,能够实现播放网络歌曲,循环播放多首音乐,下面我们来实现一下 ...
- PhpStorm添加PHP代码规范检查CodeSniffer(phpcs)和PHP代码静态分析工具Mess Detector(phpmd)
一.安装 添加镜像,加速下载 ./composer.phar config -g repo.packagist composer https://packagist.phpcomposer.com ...
- 十七,k8s集群指标API及自定义API
目录 资源指标: Metrics-Server 资源指标: Metric-Server介绍 Metric-Server部署 下载yaml文件 因为有墙, 所以提前下载image镜像, 当然也可以手动修 ...