[图中找环] Codeforces 659E 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: ,
,
,
,
.
题意:有n个点m条边,一开始是双向边,现在要改为单边,问现在入度为0的点最少有多少个
思路:如果没环就必定会出现一个城市入度为0,所以就是如何找环,一种是dfs看是否有节点被重复访问,另一种是并查集看祖先是否是它本身且是否和其他环相连
- #include<bits/stdc++.h>
- using namespace std;
- const int amn=1e5+;
- vector<int> v[amn];
- bool idx[amn];
- int ans,f;
- void dfs(int x,int pre){
- if(idx[x]){f=;return;}
- idx[x]=;
- for(int i=;i<v[x].size();i++){
- int u=v[x][i];
- if(u==pre)continue;
- dfs(u,x);
- }
- }
- int main(){
- int n,m,u,t,len,le;
- ios::sync_with_stdio();
- cin>>n>>m;
- for(int i=;i<m;i++){
- cin>>u>>t;
- v[u].push_back(t);
- v[t].push_back(u);
- }
- for(int i=;i<=n;i++){
- if(!idx[i]){
- f=;
- dfs(i,);
- if(!f)ans++;
- }
- }
- printf("%d\n",ans);
- }
dfs找环
- #include<bits/stdc++.h>
- using namespace std;
- const int amn=1e5+;
- int pre[amn];
- bool idx[amn];
- int ans;
- int fd(int x){
- return x==pre[x]?x:pre[x]=fd(pre[x]);
- }
- int main(){
- int n,m,u,v,len,le;
- ios::sync_with_stdio();
- cin>>n>>m;
- for(int i=;i<=n;i++)pre[i]=i;
- for(int i=;i<m;i++){
- cin>>u>>v;
- int fu=fd(u),fv=fd(v);
- if(fu!=fv){
- pre[fu]=fv;
- if(idx[fu]||idx[fv]||idx[u]||idx[v])idx[fu]=idx[fv]=idx[u]=idx[v]=;
- }
- else idx[fu]=idx[fv]=idx[u]=idx[v]=;
- }
- for(int i=;i<=n;i++)if(fd(i)==i&&!idx[i])ans++;
- printf("%d\n",ans);
- }
并查集找环
[图中找环] Codeforces 659E New Reform的更多相关文章
- [hdu5348]图上找环,删环
http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...
- CodeForces 659E New Reform (图的遍历判环)
Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...
- Codeforces 659E New Reform【DFS】
题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...
- 如何判断图中存环(正&负)
1.正环 用 SPFA不断的进行松弛操作,发现当前金额可以比本身大就更新,同时记录更新次数.如果更新次数超过n次,说明存在”正“环. 2.负环 这里先说明下负环.(求最短距离的时候) 在我们用SPFA ...
- CodeForces 659E New Reform
题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...
- python 图中找目标并截图
import numpy as npdef sjjt(xha,sjh,beitu,jl,xx,yy): #检查目标,并将目标指定范围内截图 pull_screenshot(xha,sjh,xx) #p ...
- codeforces 659E . New Reform 强连通
题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...
- HDU4514(非连通图的环判断与图中最长链)
题目:设计风景线 题意:给定一个无向图,图可能是非连通的,如果图中存在环,就输出YES,否则就输出图中最长链的长度. 分析:首先我们得考虑这是一个无向图,而且有可能是非连通的,那么就不能直接像求树那样 ...
- 图论 公约数 找环和链 BZOJ [NOI2008 假面舞会]
BZOJ 1064: [Noi2008]假面舞会 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1655 Solved: 798[Submit][S ...
随机推荐
- 手机视频APP将关闭 生态梦成空的三星如何自救?
生态梦成空的三星如何自救?"> 三星如今的处境,只能用"屋漏偏逢连夜雨"来形容.继营收.利润.智能手机销量等大幅下滑之后,裁员也接踵而来,股价的下跌也自然在情理之中 ...
- HTML笔记06--浮动第一章
float --浮动 一 1.啥叫浮动? [使元素向左或向右移动,其周围的元素也会重新排列]简言之,就是让盒子并排. 通过float定义浮动 ---------- 未浮动样式代码如下: ------- ...
- Spring源码阅读笔记03:xml配置读取
前面的文章介绍了IOC的概念,Spring提供的bean容器即是对这一思想的具体实现,在接下来的几篇文章会侧重于探究这一bean容器是如何实现的.在此之前,先用一段话概括一下bean容器的基本工作原理 ...
- git还原历史某一版本
返回上一版本 git reset --hard HEAD^ 常用的命令git refloggit reset --hard "填写版本号" 黄色的就是版本号 其他查看以前版本的命令 ...
- 禁止用户使用 sudo su 命令进入root 模式
禁止普通用户通过sudo su命令进入root模式的方法(在root模式下操作): 1. 修改 /etc/sudoers 的权限, 用来写入文件 # chmod 777 /etc/sudoers 2. ...
- 新冠疫情下,亚德诺(ADI)全面加速
前言:亚德诺Analog Devices, Inc.(简称ADI),公司总部设在美国马萨诸塞州诺伍德市,设计和制造基地遍布全球.ADI公司被纳入标准普尔500指数(S&P 500 Index) ...
- 《javascript高级程序设计》笔记:文档模式
文档模式是用于指定浏览器使用什么样的标准来正确的显示网页,各个标准的解析存在着差异 文档类型的分类 文档模式大致分为三种类型: 混杂模式(quirks mode) 标准模式(standards mod ...
- Canvas 使用及应用
Canvas canvas 是 HTML5 当中我最喜欢的所有新特性中我最喜欢的一个标签了.因为它太强大了,各种有意思的特效都可以实现. 1. canvas 的基本使用方法 - 它是一个行内块元素 - ...
- python学习记录_IPython基础,Tab自动完成,内省,%run命令_
这是我第一次写博客,之前也有很多想法,想把自己所接触的,以文本的形式储存,总是没有及时行动.此次下定决心,想把自己所学,所遇到的问题做个记录共享给诸位,与此同时自己作为备忘,感谢各位访问我的博 ...
- 数据库介绍以及MySQL数据库的使用
一 数据库介绍 1.1 数据库定义 数据库就是存储数据的仓库 本质上就是一套cs结构的TCP程序 客户端连接到服务器 向服务器发送指令 完成数据的操作 1.2 常见数据库 关系型数据库 就是 ...