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. 系统升级日记(3)- 升级SharePoint解决方案和Infopath

    最近一段时间在公司忙于将各类系统进行升级,其最主要的目标有两个,一个是将TFS2010升级到TFS2013,另外一个是将SharePoint 2010升级到SharePoint 2013.本记录旨在记 ...

  2. [转]史上最全的CSS hack方式一览

    做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...

  3. AVL树插入操作实现

    为了提高二插排序树的性能,规定树中的每个节点的左子树和右子树高度差的绝对值不能大于1.为了满足上面的要求需要在插入完成后对树进行调整.下面介绍各个调整方式. 右单旋转 如下图所示,节点A的平衡因子(左 ...

  4. Android中this、super的区别

    转载:http://blog.csdn.net/dyllove98/article/details/8826232 在Java中,this通常指当前对象,super则指父类的.当你想要引用当前对象的某 ...

  5. Linux 安装mysql+apache+php

    安装mysql 1.  yum install mysql mysql-server 2. 修改mysql密码 >use mysql    >update user set passwor ...

  6. ssh全屏退出的办法

    在使用ssh的时候遇到,不知道碰到哪里了,突然xshell就全屏了 解决方法 按下键盘 Alt+Enter就好了

  7. 【BZOJ 1455】罗马游戏

    左偏树模板 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...

  8. html-div中内容自动换行

    <div style='width: 100px;display:block;word-break: break-all;word-wrap: break-word;'> 内容超出div宽 ...

  9. 国外有哪些比较好的IT社区

    作者:匿名用户链接:https://www.zhihu.com/question/35373320/answer/114468522来源:知乎著作权归作者所有,转载请联系作者获得授权. Github ...

  10. Linux命令:修改文件权限命令chmod、chgrp、chown的区别

    chmod是更改文件的权限 chown是改改文件的属主与属组 chgrp只是更改文件的属组. (1)chmod是修改文件/目录的权限.可以有文字修改和数字修改. #chmod 777 /home/be ...