Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

时间:2022-12-28 00:23:56

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

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: Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图).

The second sample: Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图).

The third sample: Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图).

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=+;
int par[maxn];
bool cc[maxn]; void init(int n)
{
for(int i=;i<=n;i++)
par[i]=i;
}
int find_(int x)
{
if(x==par[x])
return x;
else
return par[x]=find_(par[x]);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init(n);
memset(cc,,sizeof(cc));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
u=find_(u);
v=find_(v);
if(u==v)
{
cc[u]=;
}
else
{
par[u]=v;
cc[v]=cc[u]||cc[v];
}
}
int ans=;
for(int i=;i<=n;i++)
{
if(par[i]==i&&!cc[i])
ans++;
}
cout<<ans<<endl;
}
return ;
}

Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)的更多相关文章

  1. Codeforces Round &num;346 &lpar;Div&period; 2&rpar; E&period; New Reform

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

  2. Codeforces Round &num;245 &lpar;Div&period; 2&rpar; B&period; Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  3. Codeforces Round &num;345 &lpar;Div&period; 1&rpar; E&period; Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  4. Codeforces Round &num;345 &lpar;Div&period; 2&rpar; E&period; Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  5. Codeforces Round &num;603 &lpar;Div&period; 2&rpar; D&period; Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  6. Codeforces Round &num;346 &lpar;Div&period; 2&rpar; E&period; New Reform dfs

    E. New Reform 题目连接: http://www.codeforces.com/contest/659/problem/E Description Berland has n cities ...

  7. Codeforces Round &num;346 &lpar;Div&period; 2&rpar;E - New Reform&lpar;DFS &plus; 好题&rpar;

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

  8. Codeforces Round &num;346 &lpar;Div&period; 2&rpar; E - New Reform 无相图求环

    题目链接: 题目 E. New Reform time limit per test 1 second memory limit per test 256 megabytes inputstandar ...

  9. Codeforces Round &num;600 &lpar;Div&period; 2&rpar; D题【并查集&plus;思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

  10. Codeforces Round &num;345 &lpar;Div&period; 2&rpar; E&period; Table Compression 并查集&plus;智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. Android手机总是提示:存储空间不足,解决方法

    手机才有16G空间,用一段时间后,总是提示内存不足,各种清理软件都没卵用. 有一个方法立即见效: 1.进入Recovery 2.格式化 SDCard 博客园首席神棍 野生菌野菜干批发 大舍大得 小舍小 ...

  2. for&period;&period;&period;in也反复执行语句,但它是用来操作对象的

    for...in也反复执行语句,但它是用来操作对象的

  3. IOS开发UI基础文本属性Attributes

    文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFo ...

  4. CSS3属性选择器与&lpar;&colon;not&rpar;选择器

    一:css3属性选择器: img[alt]{ border:2px dashed #000; } 这个选择器会匹配页面标签中任何一个含有alt属性的图片标签.   还可以通过设定属性值来缩小匹配范围: ...

  5. 基于HTTP的直播点播HLS

             HLS(HTTP Live Streaming) 是Apple在2009年发布的,可以通过普通的web服务器进行分发的新型流媒体协议.苹果官方对于视频直播服务提出了 HLS 解决方案 ...

  6. Oracle修改表空间大小

    在向orale数据库导入数据的时候报 ORA-01658: 无法为表空间 XXX中的段创建 INITIAL 区错误. Oracle我在创建表空间的时候初始化大小为200M,当数据库中数据量达到这个值, ...

  7. 从打车软件你能想到多少?盈利模式?商机?大数据?移动互联网蛋糕?生活方式改变withApp?

    物联网.云服务.大数据.. 淘宝.12306.卡通...一个产品改变一次生活方式. 打车app,无疑是改变生活方式的又一个产品.从打车软件,你能看到什么? 个人认为,打车软件值得各种投资方斥资,最重要 ...

  8. CSS3------background-size(背景图片尺寸属性)

    background-size 可以设置背景图片的大小,数值包括 长度length和百分比percentage. 并且会根据背景原点位置 background-origin 设置其图片覆盖的范围.那么 ...

  9. xtrabackup在线备份主库,搭建slave,使用gtid模式

    mysql:5.6.29xtrabackup:2.2.10master:192.168.166.129slave:192.168.166.131mysql数据目录:/data/mysqlmysql备份 ...

  10. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...