zoukankan      html  css  js  c++  java
  • 畅通工程

    畅通工程

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 31   Accepted Submission(s) : 13

    Font: Times New Roman | Verdana | Georgia

    Font Size:  

    Problem Description

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? 

    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
    

    Hint

    Hint Huge input, scanf is recommended.

    Source

    浙大计算机研究生复试上机考试-2005年
    并查集的水题:
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 int uset[1005];
     4 void MakeSet(int Len)
     5 {
     6     int i;
     7     for(i=1;i<=Len;i++)
     8         uset[i]=i;
     9     return;
    10 }
    11 int Find(int x)
    12 {
    13     if(x!=uset[x])
    14         uset[x]=Find(uset[x]);
    15     return uset[x];
    16 }
    17 
    18 int main()
    19 {
    20     int N,M,i,a,b,sum,A,B;
    21     while(scanf("%d",&N),N)
    22     {
    23         scanf("%d",&M);
    24         MakeSet(N);
    25         for(i=0;i<M;i++)
    26         {
    27             scanf("%d%d",&a,&b);
    28             A=Find(a);
    29             B=Find(b);
    30             if(A!=B)
    31                 uset[A]=B;
    32         }
    33         for(i=1,sum=0;i<=N;i++)
    34             if(uset[i]==i)
    35                 sum+=1;
    36         printf("%d
    ",sum-1);
    37     }
    38     return 0;
    39 }
    View Code

     修改:2015.6.1

     1 #include <iostream>
     2 #include <string.h>
     3 #include <stdio.h>
     4 #include <map>
     5 using namespace std;
     6 int ID[10086];
     7 void Cread(int N)
     8 {
     9     for(int i=0;i<=N;i++)ID[i]=i;
    10 }
    11 int Find(int x)
    12 {
    13     int tmp;
    14     if(ID[x]!=x)tmp=Find(ID[x]);
    15     else tmp=x;
    16     ID[x]=tmp;
    17     return tmp;
    18 }
    19 int main()
    20 {
    21     int N,M,i,j,k;
    22     while(scanf("%d",&N)&&N)
    23     {
    24         scanf("%d",&M);
    25         Cread(N);
    26         int a,b;
    27         int sign=0;
    28         for(i=0;i<M;i++)
    29         {
    30             scanf("%d%d",&a,&b);
    31             a=Find(a);
    32             b=Find(b);
    33             if(a!=b)
    34             {
    35                 sign++;
    36                 ID[a]=b;
    37             }
    38         }
    39         printf("%d
    ",N-1-sign);
    40     }
    41     return 0;
    42 }
    View Code
    转载请备注:
    **************************************
    * 作者: Wurq
    * 博客: https://www.cnblogs.com/Wurq/
    * Gitee: https://gitee.com/wurq
    **************************************
  • 相关阅读:
    Python自动化测试框架有哪些?
    Python正则表达式
    用户事务处理中同步请求与异步请求区别
    Python——数据结构—链表
    Mysql的隔离级别 以及对脏读、不可重复读、幻读的理解
    http协议及与https协议的区别
    Cookie与Session的区别
    Python——数据结构—数组
    Python—Flask写项目Mysql显示sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1038, 'Out of sort memory, consider increasing server sort buffer size')
    Python—将PyCharm中的代码提交在Gitee码云的方法
  • 原文地址:https://www.cnblogs.com/Wurq/p/3750340.html
Copyright © 2011-2022 走看看