2016NEFU集训第n+3场 E - New Reform

Description

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.

Sample Input

Input

4 32 11 34 3

Output

1

Input

5 52 11 32 32 54 3

Output

0

Input

6 51 22 34 54 65 6

Output

1

Hint

In the first sample the following road orientation is allowed: .

The second sample: .

The third sample: .

//这是一道并查集判环问题,之前做过这种类似的,但是忘了,一会巩固一下;

/*总结   *****并查集判环*****

并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示。

如何用并查集来判断一个图是否有环?

 此时的1同时是 2 3 的上级,如果接下来mix (2,3)

 必然会连成一个环

在程序中为 if(fx==fy) a[fx]=a[fy]=a[x]=a[y]=true;//即确定1 2 3 是环的一部分

  接下来mix(3,4),如果3 的环一部分,(或者是4、_find(3),_find(4))必可以每个元素都可以单向指向

如果这样。。。4 ,5 就没法都做到单向指向。。

即想每个元素都可以单向指向一定要有环。。。

******o(^▽^)o******

*/

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int pre[100005];
bool a[100005];

int _find(int x)
{
    int r=pre[x];
    while(r!=pre[r])
        r=pre[r];
    int i=x,j;
    while(pre[i]!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}

void mix(int x,int y)
{
    int fx=_find(x),fy=_find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
        if(a[fx]||a[fy]||a[x]||a[y])
        a[fx]=a[fy]=a[x]=a[y]=true;
    }
    else
    a[fx]=a[fy]=a[x]=a[y]=true;
}

int main()
{
    int n,m,ans;
    while(cin>>n>>m)
    {
        ans=0;
        for(int i=1;i<=n;i++)
        pre[i]=i;
        memset(a,false,sizeof(a));
        for(int i=1;i<=m;i++)
        {
            int a,b;
            cin>>a>>b;
            mix(a,b);
        }
        for(int i=1;i<=n;i++)
        {
            if(a[_find(i)]==false&&_find(i)==i)
            ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-20 11:21:49

2016NEFU集训第n+3场 E - New Reform的相关文章

2016NEFU集训第n+5场 A - Chinese Girls&#39; Amusement

Description You must have heard that the Chinese culture is quite different from that of Europe or Russia. So some Chinese habits seem quite unusual or even weird to us.       So it is known that there is one popular game of Chinese girls. N girls st

2016NEFU集训第n+3场 G - Tanya and Toys

Description In Berland recently a new collection of toys went on sale. This collection consists of 109 types of toys, numbered with integers from 1to 109. A toy from the new collection of the i-th type costs i bourles. Tania has managed to collect n 

暑假集训-个人赛第四场

ID Origin Title   10 / 52 Problem A SPOJ AMR10A Playground     Problem B SPOJ AMR10B Regex Edit Distance     Problem C SPOJ AMR11C Robbing Gringotts   1 / 14 Problem D SPOJ AMR10D Soccer Teams   0 / 3 Problem E SPOJ AMR10E Stocks Prediction   17 / 19

暑假集训-个人赛第三场

ID Origin Title 10 / 29 Problem A CodeForces 443B Kolya and Tandem Repeat   1 / 1 Problem B CodeForces 442A Borya and Hanabi 9 / 29 Problem C CodeForces 442B Andrey and Problem 3 / 17 Problem D CodeForces 442C Artem and Array 14 / 18 Problem E CodeFo

暑假集训-合训第二场

ID Origin Title   0 / 5 Problem A HDU 4116 Fruit Ninja   3 / 10 Problem B HDU 4127 Flood-it!     Problem C HDU 4130 Shadow 33 / 61 Problem D HDU 4161 Iterated Difference 15 / 35 Problem E HDU 4162 Shape Number 32 / 39 Problem F HDU 4165 Pills   2 / 2

Contest1692 - 2019寒假集训第三十一场 UPC 11075 Problem D 小P的国际象棋

非常简单的单点修改+区间加+区间查询.我用的是最近刚学的区间修改版本树状数组.  直接维护即可,注意修改后的单点值已经不是a[i],或者b[i],要通过区间查询求单点.不然是错的. 区间修改版本树状数组: #include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #define LL long long using namespace std; LL c_p[400

李雪:女生也能当编程高手

来源:http://burl.cc/haGmN 在强手如林的编程大赛中夺冠,被戏称为代码界的“女神”李雪:女生也能当编程高手 本报记者 原春琳  <中国青年报 >(2013年07月01日     06版) 历时约两个月的第二届微软“编程之美全国挑战赛”于近日落下帷幕.从来自清华.北大等各大高校的1.3万多名选手 中杀出重围,再到最后60人的巅峰对决,出乎所有人的意料,在这个男性向来占据绝对优势的领域,最后夺得冠军的居然是一名女生——北京邮电大学(以下简称 北邮)大三学生李雪. 此消息一经公布就

题解报告(CDUT暑期集训——第三场)

题解报告(CDUT暑期集训--第三场) A - Problem A. Ascending Rating HDU - 6319 思路:单调队列板子题?(但是弱的一批的我还是不会用(有空补上 用的滑动窗口算法 按着题解的从后往前做(ps:菜是原罪 AC代码 #include<stdio.h> #include<iostream> #include<math.h> #include<algorithm> #include<string.h> #incl

【暑期集训第一场】欧拉回路 | 思维 | 数论构造 | 容斥原理 | 线段树 | 归并排序

集训1(HDU2018 Multi-University Training Contest 2) ID A B C D E F G H I J AC O O 补题 ? O ? O 代码 & 简易题解 [A]:期望? 神仙题,留坑.. [B]:?? 同\(\text{A}\) [C]:求欧拉通路条数,以及每条的路径 小学数竞里有讲过,无向图一笔画的充要条件是有零个或两个"奇点"(偶点个数不限),"奇点"在这里就是指度为奇数的点... 其实上面两种情况就分别对应