New Reform
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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 ≤ nxi ≠ 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.

Examples
input

Copy
4 3
2 1
1 3
4 3
output

Copy
1
input

Copy
5 5
2 1
1 3
2 3
2 5
4 3
output

Copy
0
input

Copy
6 5
1 2
2 3
4 5
4 6
5 6
output

Copy
1
Note

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的更多相关文章

  1. [hdu5348]图上找环,删环

    http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...

  2. CodeForces 659E New Reform (图的遍历判环)

    Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...

  3. Codeforces 659E New Reform【DFS】

    题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...

  4. 如何判断图中存环(正&负)

    1.正环 用 SPFA不断的进行松弛操作,发现当前金额可以比本身大就更新,同时记录更新次数.如果更新次数超过n次,说明存在”正“环. 2.负环 这里先说明下负环.(求最短距离的时候) 在我们用SPFA ...

  5. CodeForces 659E New Reform

    题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...

  6. python 图中找目标并截图

    import numpy as npdef sjjt(xha,sjh,beitu,jl,xx,yy): #检查目标,并将目标指定范围内截图 pull_screenshot(xha,sjh,xx) #p ...

  7. codeforces 659E . New Reform 强连通

    题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...

  8. HDU4514(非连通图的环判断与图中最长链)

    题目:设计风景线 题意:给定一个无向图,图可能是非连通的,如果图中存在环,就输出YES,否则就输出图中最长链的长度. 分析:首先我们得考虑这是一个无向图,而且有可能是非连通的,那么就不能直接像求树那样 ...

  9. 图论 公约数 找环和链 BZOJ [NOI2008 假面舞会]

    BZOJ 1064: [Noi2008]假面舞会 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1655  Solved: 798[Submit][S ...

随机推荐

  1. Introduction Of Gradient Descent

    不是一个机器学习算法 是一种基于搜索的优化方法 作用:最小化一个损失函数 梯度上升法:最大化一个效用函数 import matplotlib.pyplot as plt import numpy as ...

  2. 服务治理与RPC · 跬步

    以前写过Django中使用zerorpc的方法,但是由于我们的Django是运行在gevent下,而zeromq需要启动一个后台进程处理消息,与gevent使用的greenlet携程是冲突的. 在Ja ...

  3. IPFS初窥

    虽然区块链有很多令人兴奋的特性,但是也有其固有的缺点.比如,文件或者长度较长的文本信息就不适合存储在链上.那么如何解决这个缺点呢?一个解决方案就是IPFS(Interplanetary File Sy ...

  4. metrics模块

    class sklearn.metrics 方法 1.分类问题的度量 metrics.accuracy_score metrics.auc metrics.f1_score metrics.preci ...

  5. pip安装psycopg2失败解决

    pip install psycopg2==2.8.4报错ERROR: Command "python setup.py egg_info" failed with error c ...

  6. Jstorm执行task报错windows CONFIG SET protected-mode no

    windows  CONFIG SET protected-mode no报错说redis受保护模式,redis使用的是Redis-x64-3.2.100,参考博文说是redis3.2之后加入的特性, ...

  7. SQL基本操作总结

    1.SQL简介 结构化查询语言 (层次模型,网状模型,关系模型) 关系模型是目前的主流 (Oralce,mysql mssql ) SQL标准:ANSI (1992 1997 2002 ISO) 方言 ...

  8. 干货--手把手撸vue移动UI框架: 滑动删除

    前言 前几天因为项目需要,用jquery写了一个swiperOut组件,然后我就随便把这个组件翻译成基于Vue的了,有兴趣的朋友可以看下.Github源码(不麻烦的话帮忙start,请各位大爷赏个星星 ...

  9. Yuchuan_Linux_C编程之六 Makefile项目管理

    一.整体大纲 二.makefile的编写 一个规则   两个函数  三个变量 1. 一个规则 三要素:目标, 依赖, 命令    目标:依赖    命令:            第一条规则是用来生成终 ...

  10. mysql数据库远程访问设置方法

    1. 修改方式1代码改表法.  可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库 ...