题目链接:

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. 好用的公共 DNS

    Google Public DNS: 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844 OpenDNS: 208.67.222.222 ...

  2. Hibernate “N+1”查询问题

    Hibernate 默认情况下使用立即检索策略,即从数据库加载A对象时  会同时加载跟它关联的B,这样产生了不必要的对象集合查询,而且本来可以合并的sql要执行1+N次,因为一条select出所有的A ...

  3. java 接口回调

    学习自:http://blog.csdn.net/xiaanming/article/details/8703708/ http://hellosure.iteye.com/blog/1130176 ...

  4. Redis(九):使用RedisTemplate访问Redis数据结构API大全

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...

  5. ubantu 下 修改mysql 默认编码

    启动mysql后,以root登录mysql root@Eadgar-virtual-machine:~# mysql -uroot -proot mysql> show variables li ...

  6. Codeforces 558(C、D、E)总结

    558C 题意:给你n个数,可对每一个数进行操作(乘2或者除以2).求最少的操作使得全部的数都相等. 思路 : dp[ t ] 表示全部的数转化到 t 所需的最少操作, vis[ t ] 表示有多少数 ...

  7. 玩转 eclipse:[1]如何快速找错-debug

    本文摘自百度经验 原文地址如下: 玩转 eclipse:[1]如何快速找错-debu eclipse是软件开发人员必备的IDE之一. 由于语言障碍或者是经验不足,许多刚刚新手并不清楚如何高效使用ecl ...

  8. 【Python】IDLE启动错误

    启动IDLE时报Subprocess Startup Error错误 错误信息 IDLE's subprocess didn't make connection.Either IDLE cant't ...

  9. 计算机器内存数量+引入和显示ARDS成员

    [1]README 1.1) 本代码在于读取内存中多个 内存段的地址范围描述符结构体(ARDS),有多少个内存段可以用: 1.2) source code and images in the blog ...

  10. Python学习总结之三 -- 优雅的字符串

    优雅的字符串 前言 记得我在Python学习总结第一篇中有提到字符串,那个可以算是先打个招呼吧,因为没有提到任何关于字符串的处理方法.今天,给大家详细讲解一下Python中字符串的使用方法,如有不当或 ...