Description

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 isnot 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 consideredseparate, 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: thei-th road is determined by two distinct integersxi, yi
(1 ≤ xi, yi ≤ n,xi ≠ yi),
wherexi andyi are the numbers of the cities connected by thei-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.

Sample Input

Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1

Hint

In the first sample the following road orientation is allowed: ,,.

The second sample: ,,,,.

The third sample: ,,,,.

题意:给出一些点和边。要求你把边变成有向边使得入度为0的点最少

分析:对于一些相连的点来说,假设形成的图中有环。那么我们一定可以从环上的某点出发使入度为0的点没有,

假设无环,那么仅仅须要一个入度为0的点就能使其它点入度不为0,那么问题就转换为推断图中是否有环了,

我们从任一个点遍历整个图,然后判环调整答案就可以

#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000"); using namespace std; #define INF 0x3f3f3f3f vector<int>v[100006];
int vis[100006];
int ans;
int flag;
void dfs(int s,int f,int pre)
{
vis[s]=1;
for(int i=0; i<v[s].size(); i++)
{
if(vis[v[s][i]]&&pre!=v[s][i]) flag=-1;
else if(!vis[v[s][i]])
{
dfs(v[s][i],f,s);
}
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(vis,0,sizeof vis);
for(int i=1; i<=n; i++) v[i].clear();
for(int i=0; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
ans=0;
for(int i=1; i<=n; i++)
{
if(!vis[i])
{
flag=0;
ans++;
dfs(i,i,i);
ans+=flag;
}
}
printf("%d\n",ans);
}
return 0;
}

CodeForces 659E New Reform (图的遍历判环)的更多相关文章

  1. [图中找环] Codeforces 659E New Reform

    New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  2. CodeForces 659E New Reform

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

  3. Codeforces 659E New Reform【DFS】

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

  4. codeforces 659E . New Reform 强连通

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

  5. 洛谷2444(Trie图上dfs判环)

    要点 并没问具体方案,说明很可能不是构造. 思考不断读入这个文本串,然后中间不出现某些文法的串.啊,这就是个自动机. 将不合法串使用ac自动机构成一个Trie图,我们需要的字符串就是在这个自动机上无限 ...

  6. cf1278D——树的性质+并查集+线段树/DFS判环

    昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...

  7. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  8. C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)

    图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...

  9. CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS

    题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...

随机推荐

  1. 【河南省多校脸萌第六场 B】点兵点将

    [链接]点击打开链接 [题意] 在这里写题意 [题解] 先每个单位都不建造bi; 打死一个ai之后,把bi加入到大根堆里面. 然后等到不够打死某个单位的时候; 从大根堆里面取出最大的那个bi;不断取, ...

  2. 51Nod——N1082 与7无关的数

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1082 题目来源: 有道难题 基准时间限制:1 秒 空间限制:13107 ...

  3. NAACL 2013 Paper Mining User Relations from Online Discussions using Sentiment Analysis and PMF

    中文简单介绍:本文对怎样基于情感分析和概率矩阵分解从网络论坛讨论中挖掘用户关系进行了深入研究. 论文出处:NAACL'13. 英文摘要: Advances in sentiment analysis ...

  4. Python 极简教程(三)数据类型

    每种语言都有各种数据类型.这就像在现实生活中,我们计数的时候需要用到数字,在表述金额.重量.距离等需要精确计数时用到小数,在日常交谈中要用文字,等等.在计算机语言中,为了表述不同的情况,也需要用到各种 ...

  5. 【Codeforces Round #301 (Div. 2) B】 School Marks

    [链接] 我是链接,点我呀:) [题意] 已知k门成绩. 总共有n门成绩. 让你构造剩下的n-k门成绩,使得这n门成绩的中位数>=y,并且这n门成绩的和要小于等于x. n为奇数 [题解] 首先判 ...

  6. DBeaver笔记-快捷键篇

    公司使用的是PostgreSQL数据库,可以使用pgAdmin或者DBeaver进行连接该数据库.个人更喜欢用DBeaver,因为其界面更加美观,操作也相对简单.对于习惯了eclipse的开发者来说, ...

  7. [Angular2 Router] Preload lzay loading modules

    From router v3.1.0, we have preloading system with router. PreloadAllModules After the init module l ...

  8. 3、Pycharm使用

    1.设置文件模板 file->settings->Editor->File and Code Templates->Python Script 2.运行 a.点击要运行的文件, ...

  9. hdu 4811 数学 不难

    http://acm.hdu.edu.cn/showproblem.php? pid=4811 由于看到ball[0]>=2 && ball[1]>=2 && ...

  10. angular自定义管道

    原文地址 https://www.jianshu.com/p/5140a91959ca 对自定义管道的认识 管道的定义中体现了几个关键点: 1.管道是一个带有“管道元数据(pipe metadata) ...