程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> PAT 05-樹7 File Transfer,pat05-

PAT 05-樹7 File Transfer,pat05-

編輯:關於C語言

PAT 05-樹7 File Transfer,pat05-


這次的題讓我對選擇不同數據結構所產生的結果驚呆了,一開始用的是結構來存儲集合,課件上有現成的,而且我也是實在不太會,150ms的時間限制過不去,不得已,看到這題剛好可以用數組,結果7ms最多,有意思!什麼是時間復雜度,終於有了一次感性的認識。這題還有個check的要求,我用了一個鏈表來存儲,感覺挺麻煩的,不知是否有更好的方法,題設要求及代碼實現如下

  1 /*
  2     Name: 
  3     Copyright: 
  4     Author: 
  5     Date: 06/04/15 09:46
  6     Description: 
  7 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?
  8 
  9 Input Specification:
 10 
 11 Each input file contains one test case. For each test case, the first line contains N (2<=N<=104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:
 12 
 13 I c1 c2  
 14 where I stands for inputting a connection between c1 and c2; or
 15 
 16 C c1 c2    
 17 where C stands for checking if it is possible to transfer files between c1 and c2; or
 18 
 19 S
 20 where S stands for stopping this case.
 21 
 22 Output Specification:
 23 
 24 For each C case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k components." where k is the number of connected components in this network.
 25 
 26 Sample Input 1:
 27 5
 28 C 3 2
 29 I 3 2
 30 C 1 5
 31 I 4 5
 32 I 2 4
 33 C 3 5
 34 S
 35 Sample Output 1:
 36 no
 37 no
 38 yes
 39 There are 2 components.
 40 Sample Input 2:
 41 5
 42 C 3 2
 43 I 3 2
 44 C 1 5
 45 I 4 5
 46 I 2 4
 47 C 3 5
 48 I 1 3
 49 C 1 5
 50 S
 51 Sample Output 2:
 52 no
 53 no
 54 yes
 55 yes
 56 The network is connected.
 57 */
 58 #include <stdio.h>
 59 #include <stdlib.h>
 60 #include <stdbool.h>
 61 
 62 typedef struct Flag
 63 {
 64     bool flag;
 65     struct Flag * next;
 66 }Flag, * pFlag;
 67 
 68 int Find(int S[], int X);
 69 void Union(int S[], int X1, int X2);
 70 
 71 int MaxSize;
 72 
 73 int main()
 74 {
 75 //    freopen("in.txt", "r", stdin); // for test
 76     int i, c1, c2, k;
 77     char ch;
 78     
 79     scanf("%d\n", &MaxSize);
 80     
 81     int S[MaxSize];
 82     for(i = 0; i < MaxSize; i++)
 83         S[i] = -1;
 84     
 85     pFlag head = (pFlag)malloc(sizeof(Flag));
 86     pFlag p, tmp;
 87     
 88     p = head;
 89     scanf("%c", &ch);
 90     while(ch != 'S')
 91     {
 92         if(ch == 'C')
 93         {
 94             pFlag f = (pFlag)malloc(sizeof(Flag));
 95             f->next = NULL;
 96             scanf("%d%d\n", &c1, &c2);
 97             if(Find(S, c1) == Find(S, c2))
 98                 f->flag = true;
 99             else
100                 f->flag = false;
101             p->next = f;
102             p = p->next;
103         }
104         else
105         {
106             scanf("%d%d", &c1, &c2);
107             Union(S, c1, c2);
108         }
109         scanf("%c", &ch);
110     }
111     
112     p = head->next;
113     tmp = head;
114     free(tmp);
115     while(p)
116     {
117         if(p->flag)
118             printf("yes\n");
119         else
120             printf("no\n");
121         tmp = p;
122         p = p->next;
123         free(tmp);
124     }
125     k = 0;
126     do
127     {
128         if(S[--MaxSize] < 0)
129             k++;
130     }while(MaxSize);    
131     if(k > 1)
132         printf("There are %d components.\n", k);
133     else
134         printf("The network is connected.\n");
135 //    fclose(stdin); // for test
136     return 0;
137 }
138 
139 int Find(int S[], int X)
140 {
141     X--;
142     for(; S[X] >= 0; X = S[X]);
143     
144     return X;
145 }
146 
147 void Union(int S[], int X1, int X2)
148 {
149     int Root1, Root2;
150     
151     Root1 = Find(S, X1);
152     Root2 = Find(S, X2);
153     if(Root1 != Root2)
154     {
155         if(S[Root1] <= S[Root2])
156         {
157             S[Root1] += S[Root2];
158             S[Root2] = Root1;
159         }
160         else
161         {
162             S[Root2] += S[Root1];
163             S[Root1] = Root2;
164         }
165     }
166 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved