畅通工程——浙大计算机研究生复试上机考试-2005年

原创


畅通工程

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

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.
 
此题为 HDU Online Judge System 上的题目,序号为1232:http://acm.hdu.edu.cn/showproblem.php?pid=1232
解题思路:
  直接使用并查集即可,此题和我(找亲戚:https://www.cnblogs.com/chiweiming/p/9310599.html)那篇博客是类似的题目,
在此不再赘述,如果两个城镇非连通,则将它们各自所在树的根结点连通即可;每次合并城镇时将城镇数-1,最后只需要输出城镇
数-1即可(N个城镇用N-1条路即可连接)。
 
Java代码:
 1 import java.util.*;
 2 
 3 public class 畅通工程 {
 4     
 5     static int city[];
 6     
 7     static int findFather(int jieDian) {    //找根节点
 8         return jieDian==city[jieDian]?jieDian:findFather(city[city[jieDian]]);
 9     }
10 
11     public static void main(String[] args) {
12         Scanner reader=new Scanner(System.in);
13         ArrayList list=new ArrayList();    //动态数组
14         int result=0;
15         int add=1;
16         int count=0;
17         int saveResult[]=new int[add];
18         while(reader.hasNext()) {
19             
20             int N=reader.nextInt();    //城镇数
21             int save_N=N;
22             int M;
23             if(N!=0) {
24                 count++;
25                 M=reader.nextInt();    //关系数
26             }
27             else {
28                 break;
29             }
30             city=new int[N+1];
31             for(int i=1;i<=N;i++) {    //首先指向本身
32                 city[i]=i;
33             }
34             for(int i=1;i<=M;i++) {    //建立城镇树
35                 int city_One=reader.nextInt();
36                 int city_Two=reader.nextInt();
37                 int tou_One=findFather(city_One);
38                 int tou_Two=findFather(city_Two);
39                 if(tou_One!=tou_Two) {    //两个城镇不连通
40                     save_N--;    //2个城镇合并成1个
41                     if(tou_One<tou_Two) {
42                         city[tou_One]=tou_Two;
43                     }
44                     else {
45                         city[tou_Two]=tou_One;
46                     }
47                 }
48             }
49             result=save_N-1;    //sava_N个城镇用sava_N-1条路即可连接
50             list.add(result);    //将结果暂存入数组
51         }
52         for(int i=0;i<count;i++) {
53             System.out.println(list.get(i));
54         }
55     }
56 
57 }
View Code

C代码:

 1 #include<stdio.h> 
 2 #include<stdlib.h>
 3 
 4 int count=0;
 5 int *arr;
 6 int flag[1005];
 7 
 8 int findFather(int jieDian){
 9     return jieDian==arr[jieDian]?jieDian:findFather(arr[ arr[jieDian] ]);    
10 }
11 
12 int main(){
13     
14     while(1){
15         
16         int N;
17         scanf("%d",&N);
18         if(N==0){
19             break;
20         }
21         int sava_N=N;
22         count++;
23         int M;
24         scanf("%d",&M);
25         arr=(int *)malloc(sizeof(int)*(N+1));
26         for(int i=1;i<=N;i++){
27             arr[i]=i;    //指向本身 
28         }
29         for(int i=1;i<=M;i++){
30             int relation_One;
31             int relation_Two;
32             scanf("%d%d",&relation_One,&relation_Two);
33             int fa_One=findFather(relation_One);
34             int fa_Two=findFather(relation_Two);
35             if(fa_One!=fa_Two) {
36                 sava_N--;
37                 if(fa_One<fa_Two){
38                     arr[fa_Two]=fa_One;
39                 }
40                 else{
41                     arr[fa_One]=fa_Two;
42                 }
43             }
44         }
45         flag[count]=sava_N-1;
46     }
47     for(int i=1;i<=count;i++){
48         printf("%d\n",flag[i]);
49     }
50     
51     return 0;
52 }
View Code

19:32:41

2018-07-16

posted @ 2018-07-16 19:33  一转身已万水千山  阅读(487)  评论(0编辑  收藏  举报