E. New Reform

题目连接:

http://www.codeforces.com/contest/659/problem/E

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 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.

Sample Input

4 3

2 1

1 3

4 3

Sample Output

1

Hint

题意

给你一个图,现在要你给这个图里面的边定方向,使得入度为0的点最少。

题解:

对于一个连通块而言,如果里面存在一个环,那么必然所有点的入度都可以大于等于1

否则的话,就存在一个点的入度为0。

dfs一波就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
vector<int> E[maxn];
int n,m,flag;
int vis[maxn];
void dfs(int x,int fa)
{
if(vis[x])
{
flag = 1;
return;
}
vis[x]=1;
for(int i=0;i<E[x].size();i++)
{
int v=E[x][i];
if(v==fa)continue;
dfs(v,x);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
int ans = 0;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
flag = 0;
dfs(i,-1);
if(!flag)ans++;
}
}
cout<<ans<<endl;
}

Codeforces Round #346 (Div. 2) E. New Reform dfs的更多相关文章

  1. Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)

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

  2. Codeforces Round #346 (Div. 2) E - New Reform 无相图求环

    题目链接: 题目 E. New Reform time limit per test 1 second memory limit per test 256 megabytes inputstandar ...

  3. Codeforces Round #346 (Div. 2) E. New Reform

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

  4. 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 ...

  5. Codeforces Round #346 (Div. 2) A Round-House

    A. Round House 题目链接http://codeforces.com/contest/659/problem/A Description Vasya lives in a round bu ...

  6. Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径

    E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...

  7. Codeforces Round #346 (Div. 2) A. Round House 水题

    A. Round House 题目连接: http://www.codeforces.com/contest/659/problem/A Description Vasya lives in a ro ...

  8. Codeforces Round #346 (Div. 2)

    前三题水 A #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; int main() { int ...

  9. Codeforces Round #346 (Div. 2) D Bicycle Race

    D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...

随机推荐

  1. callee与caller

    1.callee arguments.callee表示当前函数,使用于递归 function factorial(num){ if(num<=1){ return 1; }else{ retur ...

  2. flask插件系列之Flask-WTF表单

    flask_wtf是flask框架的表单验证模块,可以很方便生成表单,也可以当做json数据交互的验证工具,支持热插拔. 安装 pip install Flask-WTF Flask-WTF其实是对w ...

  3. Linux下多路径multipath配置【转】

    一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath  检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...

  4. Otto:EventBus

    Otto:EventBus 2014年6月20日 星期五 15:14 参考: http://www.mythroad.net/?p=4151 Otto 是Android系统的一个Event Bus模式 ...

  5. #ifndef的用法

    作用:防止头文件的重复包含和编译 定义 #ifndef x #define x ... #endif 这是宏定义的一种,它可以根据是否已经定义了一个变量来进行分支选择,一般用于调试等等.实际上确切的说 ...

  6. css控制单行文本溢出

    1.溢出属性(容器的) overflow:visible/hidden(隐藏)/scroll/auto(自动)/inherit; visible:默认值,内容不会被修剪,会成现在元素框之外: hidd ...

  7. 洛谷P2692 覆盖 题解

    题目传送门 这道题一开始想使用二维的bool型数组来存,最后统计.但看到数据范围... 所以就改用两个bool型数组(一维),分别储存横.列,最后将横.列面积求出来,再减去重复算的面积(横的个数*列的 ...

  8. [你必须知道的.NET]第二十一回:认识全面的null

    发布日期:2008.7.31 作者:Anytao © 2008 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 null.nullable.??运算符.null ...

  9. apache kafka系列之jmx监控指标参数

    https://blog.csdn.net/lizhitao/article/details/35986849

  10. android Webview Html5 相关文章

    Android WebView的使用集锦(支持Html5) http://blog.csdn.net/l_215851356/article/details/69239643 WebView详解与简单 ...