zoukankan      html  css  js  c++  java
  • [图中找环] Codeforces 659E New Reform

    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
    Copy
    4 3
    2 1
    1 3
    4 3
    output
    Copy
    1
    input
    Copy
    5 5
    2 1
    1 3
    2 3
    2 5
    4 3
    output
    Copy
    0
    input
    Copy
    6 5
    1 2
    2 3
    4 5
    4 6
    5 6
    output
    Copy
    1
    Note

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

    The second sample: .

    The third sample: .

    题意:有n个点m条边,一开始是双向边,现在要改为单边,问现在入度为0的点最少有多少个

    思路:如果没环就必定会出现一个城市入度为0,所以就是如何找环,一种是dfs看是否有节点被重复访问,另一种是并查集看祖先是否是它本身且是否和其他环相连

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int amn=1e5+5;
     4 vector<int> v[amn];
     5 bool idx[amn];
     6 int ans,f;
     7 void dfs(int x,int pre){
     8     if(idx[x]){f=1;return;}
     9     idx[x]=1;
    10     for(int i=0;i<v[x].size();i++){
    11         int u=v[x][i];
    12         if(u==pre)continue;
    13         dfs(u,x);
    14     }
    15 }
    16 int main(){
    17     int n,m,u,t,len,le;
    18     ios::sync_with_stdio(0);
    19     cin>>n>>m;
    20     for(int i=0;i<m;i++){
    21         cin>>u>>t;
    22         v[u].push_back(t);
    23         v[t].push_back(u);
    24     }
    25     for(int i=1;i<=n;i++){
    26         if(!idx[i]){
    27             f=0;
    28             dfs(i,0);
    29             if(!f)ans++;
    30         }
    31     }
    32     printf("%d
    ",ans);
    33 }
    dfs找环
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int amn=1e5+5;
     4 int pre[amn];
     5 bool idx[amn];
     6 int ans;
     7 int fd(int x){
     8     return x==pre[x]?x:pre[x]=fd(pre[x]);
     9 }
    10 int main(){
    11     int n,m,u,v,len,le;
    12     ios::sync_with_stdio(0);
    13     cin>>n>>m;
    14     for(int i=1;i<=n;i++)pre[i]=i;
    15     for(int i=0;i<m;i++){
    16         cin>>u>>v;
    17         int fu=fd(u),fv=fd(v);
    18         if(fu!=fv){
    19             pre[fu]=fv;
    20             if(idx[fu]||idx[fv]||idx[u]||idx[v])idx[fu]=idx[fv]=idx[u]=idx[v]=1;
    21         }
    22         else idx[fu]=idx[fv]=idx[u]=idx[v]=1;
    23     }
    24     for(int i=1;i<=n;i++)if(fd(i)==i&&!idx[i])ans++;
    25     printf("%d
    ",ans);
    26 }
    并查集找环
  • 相关阅读:
    20155202 实验四 Android开发基础
    20155202 《Java程序设计》实验三(敏捷开发与XP实践)实验报告
    20155202 2016-2017-2 《Java程序设计》第10周学习总结
    20155202 2016-2017-2 《Java程序设计》第9周学习总结
    20155202 《Java程序设计》实验二(面向对象程序设计)实验报告
    20155334 9月27日课堂检测补交
    20155334 2017-2018-1《信息安全系统设计基础》第一周学习总结
    20155334 2016-2017-2《Java程序设计》课程总结
    20155334 实验五 Java网络编程及安全
    20155334 实验四:Android程序设计
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11363141.html
Copyright © 2011-2022 走看看