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. bat批处理文件启动Eclipse和ivy本地仓库的配置

    一.bat批处理文件启动Eclipse 所需文件: 1.eclipse 2.jre 3.startup-eclipse.bat 确保以上三个文件夹同级 startup-eclipse.bat: set ...

  2. hiho一下 第六十四周 Right-click Context Menu

    题目链接:hihocoder 第64周 题意概述: 上下文菜单是panel(面板)包括很多section(分区),一个分区里面至少包含一个菜单项.每一个菜单项都对应有一个子panel,这个panel可 ...

  3. [Ajax系列]Ajax介绍

    Ajax简介: Ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. What ? AJAX=异步JavaScript和XML AJAX是一种用于创建快读动态网页的技术 通过在后台语 ...

  4. linux基础-第十三单元 硬盘分区、格式化及文件系统的管理二

    第十三单元 硬盘分区.格式化及文件系统的管理二 文件系统的挂载与卸载 什么是挂载 mount命令的功能 mount命令的用法举例 umount命令的功能 umount命令的用法举例 利用/etc/fs ...

  5. 把php上传sae问题要使用IO

    应用移植指南 一,为什么要移植应用 SAE禁止IO写操作,代码目录不能写入.这意味着普通程序的上传图片.生成缓存等操作都不能在SAE上正常运行,这时候你需要对这些代码进行修改后才能让你的程序运行在SA ...

  6. Edge Model

    三种edge Step (阶梯) Ramp (坡) Roof 因为噪声的存在, ramp edge是最常见的. 一阶和二阶算子在ramp edge处的表现 一阶和二阶算子对噪声的敏感性 从上自下, 高 ...

  7. Kernel Methods (2) Kernel function

    几个重要的问题 现在已经知道了kernel function的定义, 以及使用kernel后可以将非线性问题转换成一个线性问题. 在使用kernel 方法时, 如果稍微思考一下的话, 就会遇到以下几个 ...

  8. Extjs-Ext.Ajax.request设置超时

    ExtJs的Ajax提交主要是:Ext.Ajax.request或form1.getForm().submit,超时时间默认是30秒. 很多时候,后台处理比较多,往往需要超出30秒的限制.此时,可以通 ...

  9. MVC3中使用RadioButtonFor()

    创建页面  进行初始化   默认 男 被选中 <div class="label"> <div class="editor-label"> ...

  10. 【转】wait,notify,notifyAll,join,yield,sleep的区别和联系

    1.  Thread.sleep(long) 和Thread.yield()都是Thread类的静态方法,在调用的时候都是Thread.sleep(long)/Thread.yield()的方式进行调 ...