E. New Reform

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 ≤ n, xi ≠ 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.

Input


Output


题意:n个城市,m条双向路,将这些路改成单向的,如果一个城市没有通向它的路,(入度为0)就说明该城市是单独的。问修改后最少有几个单独的城市,要使结果最小。

思路:

1.可以建成一个有向图,可能有k个联通块,如果一个联通块没有环,就说明这个联通块,至少有一个城市单独的,因此就化成找联通块和环的问题
2.联通块的话可以用并查集来维护,然后用cir[maxn]数组来标记是否有环,如果这个联通块的根节点存在环,那么该联通块不存在单独的城市,如果不存在环的话cnt++,最后的cnt就是答案;

AC代码:

#include<bits/stdc++.h>

using namespace std;
#define N 150000
int f[N];
int arr[N];
int getf(int v){
    if(v==f[v]){
        return f[v];
    }
    f[v]=getf(f[v]);
    return f[v];
}
void merge(int u,int v){
    int t1=getf(u);
    int t2=getf(v);
    if(t1!=t2){
        f[t2]=t1;
        if(arr[t2]){
            arr[t1]=t2;
        }
    }else{
        arr[t1]=;
    }
}
int n,m;
void init(){
    ;i<=n;i++)
        f[i]=i;
}
int main(){

    cin>>n>>m;
    init();
    ;
    ;i<=m;i++){
        int x,y;
        cin>>x>>y;
        merge(x,y);
    }
    ;i<=n;i++){
        if(f[i]==i&&!arr[i]){
            ans++;
        }
    }
    cout<<ans;
    ;
}

Codeforces Round #346 (Div. 2) E题 并查集找环的更多相关文章

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

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

  2. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  3. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  4. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  5. Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

  6. Codeforces Round #623 (Div. 2) D.Recommendations 并查集

    ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...

  7. Codeforces Round #346 (Div. 2) A题 [一道让我生气的思维题·]

    A. Round House Vasya lives in a round building, whose entrances are numbered sequentially by integer ...

  8. Codeforces Round #346 (Div. 2) C题

    C. Tanya and Toys In Berland recently a new collection of toys went on sale. This collection consist ...

  9. Codeforces Round #346 (Div. 2) B题

    B. Qualifying Contest Very soon Berland will hold a School Team Programming Olympiad. From each of t ...

随机推荐

  1. 【AtCoder】AGC007

    AGC007 A - Shik and Stone 如果i + j走过的格子只有一个,那么就是可以走到 #include <bits/stdc++.h> #define fi first ...

  2. dede时间标签

    dedecms首页时间标签:1.12-27 样式([field:pubdate function='strftime("%m-%d",@me)'/]) 2.May 15, 2009 ...

  3. Django(一)安装启动

    Django下载/启动 1.下载安装 pip install django 或者 压缩文件下载地址:https://github.com/django/django/releases python s ...

  4. decimal, double, float

     更新: 2019-09-08  c# and js 要 ceil floor 2 decimal point 都没有 build in 的 solution 比如 15.667 想 ceil to ...

  5. Matlab函数kmeans

    Matlab函数kmeans K-means聚类算法采用的是将N*P的矩阵X划分为K个类,使得类内对象之间的距离最大,而类之间的距离最小. 使用方法:Idx=Kmeans(X,K)[Idx,C]=Km ...

  6. QT QListWidget去掉滚动条

    1.去掉滚动条 设置样式  包含背景色等更改 setStyleSheet("QListWidget{color:gray;font-size:12px;background:#FAFAFD; ...

  7. QT调用CHM方法

    QDesktopServices desktopServices;QString strUrl=QCoreApplication::applicationDirPath () ;strUrl=QStr ...

  8. vue关于路由容易忽略的点

    1.去掉导航里的# 在router.js中 export default new Router{ mode:'history' } 2.指定激活项的class 在router.js中 export d ...

  9. 判断Actiivty是否已经被销毁

    一般会遇到这样的情况:在一个Activity中启动一个异步任务,异步任务中需要返回值,然后被Activity使用,但是当异步任务还未结束时,按下home键,如果这个时候系统内存比较紧张,这个Activ ...

  10. Python学习记录4-列表、元祖和集合

    list列表 一组由有序数据组成的序列 数据有先后顺序 数据可以不是一类数据 list的创建 直接创建,用中括号创建,内容直接用英文逗号隔开 使用list创建 列表包含单个字符串的时候是一个特例 # ...