zoukankan      html  css  js  c++  java
  • HDU 1232:流问题(并检查集合)

    畅通project



    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 29362    Accepted Submission(s): 15452


    Problem Description
    某省调查城镇交通状况。得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通project”的目标是使全省不论什么两个城镇间都能够实现交通(但不一定有直接的道路相连,仅仅要互相间接通过道路可达就可以)。问最少还须要建设多少条道路?
     

    Input
    測试输入包括若干測试用例。每一个測试用例的第1行给出两个正整数。各自是城镇数目N ( < 1000 )和道路数目M。随后的M行相应M条道路,每行给出一对正整数,各自是该条道路直接连通的两个城镇的编号。

    为简单起见,城镇从1到N编号。
    注意:两个城市之间能够有多条道路相通,也就是说
    3 3
    1 2
    1 2
    2 1
    这样的输入也是合法的
    当N为0时,输入结束,该用例不被处理。

     

    Output
    对每一个測试用例,在1行里输出最少还须要建设的道路数目。


     

    Sample Input
    4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
     

    Sample Output
    1 0 2 998

    题目是中文的。

    所以也就不用多讲了。。题意就是要求连通的图。

    看有多少断节点数。。而要修的路也就是断节点数-1;

    实现代码例如以下:


    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<sstream>
    #include<cmath>
    
    using namespace std;
    
    #define f1(i, n) for(int i=0; i<n; i++)
    #define f2(i, n) for(int i=1; i<=n; i++)
    #define f3(i, n) for(int i=n; i>=1; i--)
    #define f4(i, n) for(int i=1; i<n; i++)
    #define M 1005
    
    int f[M];
    int ans;
    int n, m;
    int a, b;
    
    int find(int x)
    {
        return f[x] == x ? x:f[x] = find( f[x] );
    }
    
    void make(int a, int b)
    {
        int x = find(a);
        int y = find(b);
        if( x!=y )
            f[y] = x;
    }
    
    int main()
    {
        while(scanf("%d", &n) &&n )
        {
            ans = 0;
            f2(i, n)
               f[i] = i;
            scanf("%d", &m);
            while( m-- )
            {
                scanf("%d%d", &a, &b);
                make(a, b);
            }
            f2(i, n)
                if( f[i]==i )
                    ans++;    //断节点个数
            printf("%d
    ", ans-1);   //修的路等于断节点数-1
        }
    
        return 0;
    }
    



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    spring cloud alibaba +seata 实战中Error processing condition on io.seata.spring.boot.autoconfigure.问题总结
    Docker部署Elasticsearch及安装后自动关闭的问题
    SpringBoot10:Web开发静态资源处理
    SpringBoot09:整合MyBatis
    SpringBoot08:整合Druid
    SpringBoot07:整合JDBC
    SpringBoot06:自定义starter
    SpringBoot05:自动配置原理
    SpringBoot04:JSR303数据校验及多环境切换
    SpringBoot03:yaml配置注入
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4809809.html
Copyright © 2011-2022 走看看