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

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

The second sample: .

The third sample: .

题意:n个城市,m条双向道路,现在将双向改成单向,求入度 为0的最少有几个

如果是环的话就是0,如果以x点出发深搜 发现能搜到的点已经访问过此事 x 点的入读可以不是0了,因为从那个访问过的点出发能访问到x,如果以x出发所以的点都没访问过,那么x的入读为0以x作为原点来指向那些点们

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int Max = + ;
struct Edge
{
int to;
int Next;
};
Edge edge[Max * ];
int n, m;
int tot, head[Max], vis[Max];
bool hasVisit(int u, int fa)
{
vis[u] = ;
bool flag = false;
for (int i = head[u]; i != -; i = edge[i].Next)
{
int v = edge[i].to;
if (fa == v)
continue;
if (vis[v])
{
flag = true;
continue;
}
if (hasVisit(v, u))
flag = true;
}
return flag;
}
void addEdge(int u, int v)
{
edge[tot].to = v;
edge[tot].Next = head[u];
head[u] = tot++; edge[tot].to = u;
edge[tot].Next = head[v];
head[v] = tot++;
}
int main()
{
scanf("%d%d", &n, &m);
int u, v;
memset(head, -, sizeof(head));
tot = ;
for (int i = ; i < m; i++)
{
scanf("%d%d", &u, &v);
addEdge(u, v);
}
int ans = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= n; i++)
{
if (vis[i])
continue;
if (!hasVisit(i, -))
ans++;
}
//cout << ans << endl;
printf("%d\n", ans);
return ;
}
/* 但是如果输入
2 2
1 2
2 1
按说 1 2 和 2 1是一样的但是结果却是 0
*/

Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)的更多相关文章

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

    E. New Reform 题目连接: http://www.codeforces.com/contest/659/problem/E Description Berland has n cities ...

  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 #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  6. Codeforces Round #298 (Div. 2) A、B、C题

    题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...

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

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

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

随机推荐

  1. 各地IT薪资待遇讨论

    作为一个搞.net开发的程序员,在北京混了三年半,最近准备辞职到上海找工作.由于对上海的IT行业还不是很了解,在这里想让上海的同行们说下你们的情况,以方便我对自己在上海的定位,当然,其余城市的的同行们 ...

  2. HTML5+JS 《五子飞》游戏实现(五)移动棋子

    上一章 我们知道了怎么处理两个重要的吃棋动作,想要吃对方的棋子,首先得移动自己的棋子.现在里沃特跟大家分享分享,怎么移动棋子. 想要移动棋子,在页面上,首先要点击一下要移动的棋子,然后再点击一下目标位 ...

  3. android animation中的参数interpolator详解

      android:interpolator interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果可以 accelerated(加速),decelerated(减速), ...

  4. (十七)迭代器模式详解(foreach的精髓)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 各位好,很久没以LZ的身份和 ...

  5. DSOFramer 之一:在 64 位系统注册 DSOFramer

    DSOFramer是微软提供的一款用于在线编辑.调用Word.Excel等Office程序的ActiveX组件.很多第三方的Office组件都是基于DSOFramer组件开发的.今天我们不讲如何使用D ...

  6. SQLite常用点滴总结(转)

    expressionexpr ::=     expr binary-op expr |expr [NOT] like-op expr [ESCAPE expr] |unary-op expr |( ...

  7. js中字符串和数组相互转化的方法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #e4af0a } p. ...

  8. Html.BeginForm

    该方法用于构建一个From表单的开始,他的构造方法为: Html.BeginForm("ActionName","ControllerName",FormMet ...

  9. javascript代码片段

    DOMReady函数,只要DOM结构加载完成即可,不必等待所有资源加载完成,节约时间,"DOMContentLoaded"在H5中被标准化 var DOMReady=functio ...

  10. stack 栈的实现

    今天晚上去「南哪」听了场AI的讲座,除了话筒真心不给力之外,算是对微软这方面的进展有了更多了解,毕竟是半宣传性质的活动吧. 光听这些是没用的,眼下还是打好基础,多尝试学点新技术,拓宽能力和视野比较重要 ...