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. Redis 学习笔记(篇五):对象(RedisObject)

    Redis-对象 在以前的文章中,我们介绍了 Redis 用到的主要数据结构,比如简单动态字符串.双端链表.字典.压缩列表.整数集合. 然而 Redis 并没有直接使用这些数据结构来实现键值对的数据库 ...

  2. vue去掉链接中的#

    在router.js中修改, const router = new VueRouter({ mode: 'history', routes: [...] })

  3. 洛谷 P4198 楼房重建 线段树维护单调栈

    P4198 楼房重建 题目链接 https://www.luogu.org/problemnew/show/P4198 题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上 ...

  4. 基本mysql语句

    一 select语句 基本语法 select 列名1,列名2             //可以使用完全限定的列名  tables.列名 form tables 过滤(where ) 分组(group ...

  5. Photon Server 实现注册与登录(一) --- Hibernate整合到项目中

    本系列实现目的:基于Photon Server实现注册于登录 一.拷贝Nbibernate项目的文件到MyGamerServer项目中. 二.数据库新建表,结构如下 三.修改文件名和配置 (1).将拷 ...

  6. Photon Server初识(四) --- 部署自己的服务Photon Server

    准备工作: 1.一台 window 虚拟机(本机是window也行) 2.下载SDK : https://www.photonengine.com/zh-CN/sdks#server 一:SDK介绍 ...

  7. mydumper,myloader原理及实战

    mydumper 特性 (1)多线程备份(和mysqlpump的多线程不同,mysqlpump多线程备份的粒度是表,mydumper多线程备份的粒度是行,这对于备份大表特别有用)(2)因为是多线程逻辑 ...

  8. 洛谷P1087 FBI树

    P1087 FBI树题解: 看到这个题,我想到了线段树!(毕竟刚搞完st表...) 当然,题解中有位大佬也用的线段树,但是当时看的时候我看见了9个if,当场去世. 那么这是一个不用暴力的线段树,且简单 ...

  9. C语言存30位数字长的十进制方法

    题目:将一个长度最多为30位数字的十进制非负整数转换为二进制数输出. 首先: 1,30位数字的十进制,并没有一个数据类型可以存下30位的整数类型数字,所以考虑用字符串存储这个数据,遍历这个字符串,每个 ...

  10. lesson10总结

    package lesson10; public class Fa { String name="I am Fa"; static{  System.out.println(&qu ...