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. Scala入门之Array

    /** * 大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: * 1,数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储现在比较经典的是使用Hadoop,也有很多情况使用Kaf ...

  2. Codeforces Round #369(div 2)

    A:=w= B:=w= C:题意:有一排树,有的树已经上色,有的树没有上色,只能给没上色的树上色,一共m种颜色,不同的树上不同的色花费不同,涂完色后,连续颜色的树成为一段.对于给定的段数k,求出最小花 ...

  3. 准标识符(Quasi-dientifier, QI)

    Quasi-identifier From Wikipedia, the free encyclopedia Quasi-identifiers are pieces of information t ...

  4. Spring + SpringMVC + MyBatis

    1.需求说明实现用户通过数据库验证登录需求,采用Myeclipse+Tomcat 6.0+Mysql 5.0+JDK 1.6 2.数据库表开发所用是Mysql数据库,只建立单张用户表T_USER,表结 ...

  5. hibernate用setResultTransformer转换

    当你用hibernate查出数据,但是类型不是原来的类型怎么办,新增的实体类还可以用,query.setResultTransformer(Transformers.aliasToBean(AA.cl ...

  6. MySql数据类型问题

    1. mysql时间函数 DATE_ADD(now(), INTERVAL 1 DAY) AS tomorrow DATE_SUB(now(), INTERVAL 1 DAY) AS yesterda ...

  7. applicationContext.xml和dispatcher-servlet.xml的区别

    在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢? Spring 官方文 ...

  8. Javaweb容器的四种作用域

    几乎所有web应用容器都提供了四种类似Map的结构:application session request page,Jsp或者Servlet通过向着这四个对象放入数据,从而实现Jsp和Servlet ...

  9. Maven-pom.xml详解

    (看的比较累,可以直接看最后面有针对整个pom.xml的注解) pom的作用 pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵 ...

  10. Json-遍历

    1.js遍历 var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13} ...