博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #346 (Div. 2) E. New Reform
阅读量:5340 次
发布时间:2019-06-15

本文共 3210 字,大约阅读时间需要 10 分钟。

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

 

一开始觉得 是联通分量的个数-1  后来画图发现,每个联通分量如果存在环,那么他就没有 separate点。

一开始用并查集做,没调出来.....好弱..

/* ***********************************************Author        :Created Time  :2016/4/2 16:53:35File Name     :346e.cpp************************************************ */#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ull unsigned long long#define ll long long#define mod 90001#define INF 0x3f3f3f3f#define maxn 100010#define cle(a) memset(a,0,sizeof(a))const ull inf = 1LL << 61;const double eps=1e-5;using namespace std;priority_queue
,greater
>pq;struct Node{ int x,y;};struct cmp{ bool operator()(Node a,Node b){ if(a.x==b.x) return a.y> b.y; return a.x>b.x; }};bool cmp(int a,int b){ return a>b;}int vis[maxn];int n,m;vector
v[maxn];int mark=0;void dfs(int u,int fa){ if(vis[u]){ mark=1;return; } vis[u]=1; for(int i=0;i
>n>>m){ cle(vis);mark=0; for(int i=1;i<=m;i++){ scanf("%d %d",&x,&y); v[x].push_back(y); v[y].push_back(x); } int ans=0; for(int i=1;i<=n;i++){ mark=0; if(!vis[i]){ dfs(i,-1); if(mark==0)ans++; } } cout<
<

 

转载于:https://www.cnblogs.com/pk28/p/5348031.html

你可能感兴趣的文章
LeetCode-Strobogrammatic Number
查看>>
luoguP3414 SAC#1 - 组合数
查看>>
五一 DAY 4
查看>>
(转)接口测试用例设计(详细干货)
查看>>
【译】SSH隧道:本地和远程端口转发
查看>>
win8.1安装Python提示缺失api-ms-win-crt-runtime-l1-1-0.dll问题
查看>>
图片点击轮播(三)-----2017-04-05
查看>>
判断两个字符串是否相等【JAVA】
查看>>
直播技术细节3
查看>>
《分布式服务架构:原理、设计于实战》总结
查看>>
java中new一个对象和对象=null有什么区别
查看>>
字母和数字键的键码值(keyCode)
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
Spring mvc初学
查看>>
有意思的代码片段
查看>>
C8051开发环境
查看>>
VTKMY 3.3 VS 2010 Configuration 配置
查看>>
01_1_准备ibatis环境
查看>>
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
查看>>
JavaScript中的BOM和DOM
查看>>