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 ...
随机推荐
- C#读写操作app.config中的数据
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connecti ...
- 路由器WAN口IP显示为10、100、172开头,网络被电信联通等运营商做了NAT转发
摘要:路由器WAN口IP显示为10.100.172开头,网络被电信联通等运营商做了NAT转发 ... 路由器WAN口IP显示为10.100.172开头的解决方法方法一:找电信(10000号)或者联通( ...
- php对象转换为数组的部分代码
function object_array($array){ if(is_object($array)){ $array = (array)$array; } if(is_array($array)) ...
- SQL连接(join)
INNER JOIN:如果表中有至少一个匹配,则返回行 LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行 RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行 FULL JOIN ...
- 在Pytorch上使用稀疏矩阵
在Pytorch上使用稀疏矩阵 最近在写一个NLP的小项目,用到了Pytorch做神经网络模型.但是众所周知NLP的一个特点就是特征矩阵是稀疏矩阵,当时处理稀疏矩阵用的是scipy.sparse,现在 ...
- Python中import导入上一级目录模块及循环import问题的解决
转自:https://www.cnblogs.com/sjy18039225956/p/9265461.html 使用python进行程序编写时,经常会使用第三方模块包.这种包我们可以通过python ...
- nuxt项目打包上线之二
之前写过一篇nuxt打包上线的文章,请看这里:https://www.cnblogs.com/daisygogogo/p/11218809.html 上一篇文章的部署流程有点不好的地方,就是它适用于只 ...
- MySQL5.7 启动报错:initialize specified but the data directory has files in it. Aborting.
$ vi /etc/my.cnf ## datadir=/var/lib/mysql, 这个是data保存目录,进入/var/lib/mysql后,查看到确实有数据. #解决方法:将/var/lib/ ...
- Asp .Net Core 2.0 登录授权以及前后台多用户登录
用户登录是一个非常常见的应用场景 .net core 2.0 的登录方式发生了点变化,应该是属于是良性的变化,变得更方便,更容易扩展. 配置 打开项目中的Startup.cs文件,找到Configur ...
- list列表的使用
Python最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 list1 = [1,2,3,4,5,6,7,8,9] #创建列表 z = list([1,2,3,4,5,6,7,8 ...