题目链接:

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

题意:

给一个图的描述,注意有没有连接的点,把双向边变成单向边,问最后最少有多少个点没有边连向它;

思路:

贪心可以发现,没有连接的点首先就是,然后形成环的最后都能有边连向它们,然后就是像拓扑排序那样减入度,删边找到最后剩下的那些点;

AC代码:

/*
2014300227 659E - 36 GNU C++11 Accepted 78 ms 6668 KB
*/ #include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int n,m,x,y,flag[N],num[N],vis[N];
vector<int>ve[N];
queue<int>qu;
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
ve[x].push_back(y);
ve[y].push_back(x);
num[x]++;
num[y]++;
}
for(int i=;i<=n;i++)
{
if(num[i]==)
{
qu.push(i);
}
}
while(!qu.empty())
{
int to=qu.front();
vis[to]=;
qu.pop();
if(num[to]){
flag[to]=;
int len=ve[to].size(),b;
for(int i=;i<len;i++)
{
b=ve[to][i];
if(!vis[b])
{
num[b]--;
if(num[b]==)
{
qu.push(b);
}
break;
}
}
}
}
int ans=;
for(int i=;i<=n;i++)
{
if(flag[i]==&&num[i]==)
{
ans++;
}
}
cout<<ans<<"\n"; return ;
}

codeforces 659E E. New Reform(图论)的更多相关文章

  1. CodeForces 659E New Reform

    题意:给你一个无向图,如今要求你把边改成有向的. 使得入度为0的点最少,输出有多少个点入度为0 思路:脑补一波结论.假设有环的话显然没有点入度为0,其余则至少有一个点入度为0,然后就DFS一波就能够了 ...

  2. Codeforces 659E New Reform【DFS】

    题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...

  3. CodeForces 659E New Reform (图的遍历判环)

    Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...

  4. [图中找环] Codeforces 659E New Reform

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

  5. codeforces 659E . New Reform 强连通

    题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...

  6. codeforces 723E:One-Way Reform

    Description There are n cities and m two-way roads in Berland, each road connects two cities. It is ...

  7. Codeforces 444A DZY Loves Physics(图论)

    题目链接:Codeforces 444A DZY Loves Physics 题目大意:给出一张图,图中的每一个节点,每条边都有一个权值.如今有从中挑出一张子图,要求子图联通,而且被选中的随意两点.假 ...

  8. 【codeforces 732F】Tourist Reform

    [题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...

  9. 【codeforces 723E】One-Way Reform

    [题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...

随机推荐

  1. 利用动态图添加Loading动画

    opacity:CSS3中的属性,调节透明度,一般取值0.5 添加思想: 1.对超链接添加点击事件,通过new {@onclick="showLoading()"} Html.Ac ...

  2. Oracle的主键约束、唯一约束与外键约束

    http://www.shangxueba.com/jingyan/122163.html主键:  1.主键约束: 一个表只能有一个主键约束.主键可以是单个字段,也可以是多个字段.无论是哪种情况,其所 ...

  3. dm8148 videoM3 link源代码解析

    样例:从A8送一帧jpeg图片到videoM3解码,然后在将解码的数据传递到A8, 这个流程涉及的link源代码例如以下: dm8148 link之间数据传递 1)在A8上调用IpcBitsOutLi ...

  4. 菜鸟运维笔记:配置Apache二级域名及WWW訪问

    事实上www訪问也能够理解为二级域名的一种. 域名 比方.com..org..edu..gov..info..net等等都是一级域名,或称顶级域名. 其详细格式是 *.顶级域名,比方csdn.net. ...

  5. java 内存与内存溢出

    学习自:http://www.codeceo.com/article/jvm-memory-overflow.html 讲的很清楚

  6. Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!

    using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...

  7. coreos 之flannel

    提要: coreos 中 flannel 工具是coreos 网络划分工具.通过flannel 划分子网并向etcd 注册网络信息.可以做到宿主机集群中容器间网络通信. 1. 启动etcd2 服务: ...

  8. erlang和golang的比较

    1)垃圾回收GC 像 Java 一样,Go 的垃圾回收是全局的,这意味着一旦垃圾回收被触发,所有的 goroutine 都会被暂停,造成一段时间的业务延迟. Erlang 的垃圾回收是进程级别的,每一 ...

  9. 深入Asyncio(五)Event Loop

    Event Loop loop除了处理协程间的切换与结束时的异常捕捉,还要监听socket和文件描述符.先做个小测试: >>> import asyncio >>> ...

  10. 03 redis之string类型命令解析

    Redis字符串类型的操作 set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] 如: set a 1 ex 10 , 10秒有效 Set a 1 px 9000 , ...