PAT 05-tree 7 File Transfer, pat05-

Source: Internet
Author: User

PAT 05-tree 7 File Transfer, pat05-

This question made me stunned by the results of the selection of different data structures. At the beginning, I used the structure to store the collection. The courseware was ready-made and I was not very familiar with it, if the time limit of MS is too long, you have to use an array. The result is the most 7 ms, which is interesting! What is time complexity? Finally, I have a perceptual knowledge. There is also a check requirement for this question. I have used a linked list for storage, which is quite troublesome. I wonder if there is a better way. The question setting requirements and code implementation are as follows:

  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             else100                 f->flag = false;101             p->next = f;102             p = p->next;103         }104         else105         {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         else120             printf("no\n");121         tmp = p;122         p = p->next;123         free(tmp);124     }125     k = 0;126     do127     {128         if(S[--MaxSize] < 0)129             k++;130     }while(MaxSize);    131     if(k > 1)132         printf("There are %d components.\n", k);133     else134         printf("The network is connected.\n");135 //    fclose(stdin); // for test136     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         else161         {162             S[Root2] += S[Root1];163             S[Root1] = Root2;164         }165     }166 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.