This question let me to choose different data structure to produce the result stunned, a beginning with the structure to save the collection, courseware on the ready, and I also really do not, 150ms time limit pass, have to see this question just can use array, result 7ms most, interesting! What is the complexity of time, finally have a perceptual understanding. This question also has a check request, I used a chain list to store, feel very troublesome, do not know whether there is a better way, the problem set requirements and code implementation as follows
1 /*2 Name:3 Copyright:4 Author:5 DATE:06/04/15 09:466 Description:7 We have a network of computers and a list of bi-directional connections. Each of the 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:Ten One Each input file contains the one test case. For each test case, the first line contains N (2<=n<=104) and the total number of computers in a network. Each of the computer in the network are then represented by a positive integer between 1 and N. Then in the following lines, the input was given in the format: A - I C1 C2 - where I stands for inputting a connection between C1 and C2; the - C C1 C2 - where C stands for checking if it's possible to transfer files between C1 and C2; - + S - where S stands for stopping the this case. + A Output Specification: at - for each C case, print in one line the word "yes" or "no" if it's possible or impossible to transfer files between C1 and C2, respectively. At the end of all case, print on one line ' the network is connected. ' If there is a path between any pair of computers; Or "there is K." Where k is the number of connected the. - - Sample Input 1: - 5 - C 3 2 in I 3 2 - C 1 5 to I 4 5 + I 2 4 - C 3 5 the S * Sample Output 1: $ NoPanax Notoginseng No - Yes the there is 2 components. + Sample Input 2: A 5 the C 3 2 + I 3 2 - C 1 5 $ I 4 5 $ I 2 4 - C 3 5 - I 1 3 the C 1 5 - SWuyi Sample Output 2: the No - No Wu Yes - Yes About The network is connected. $ */ -#include <stdio.h> -#include <stdlib.h> -#include <stdbool.h> A +typedefstructFlag the { - BOOLFlag; $ structFlag *Next; the}flag, *PFlag; the the intFind (intS[],intX); the voidUnion (intS[],intX1,intX2); - in intMaxSize; the the intMain () About { the //freopen ("In.txt", "R", stdin);//For Test the intI, C1, C2, K; the Charch; + -scanf"%d\n", &MaxSize); the Bayi intS[maxsize]; the for(i =0; i < MaxSize; i++) theS[i] =-1; - -PFlag head = (pFlag)malloc(sizeof(Flag)); the PFlag p, tmp; the thep =head; thescanf"%c", &ch); - while(ch! ='S') the { the if(ch = ='C') the {94PFlag f = (PFlag)malloc(sizeof(Flag)); theF->next =NULL; thescanf"%d%d\n", &C1, &C2); the if(Find (S, c1) = =Find (S, C2))98F->flag =true; About Else -F->flag =false;101P->next =F;102p = p->Next;103 }104 Else the {106scanf"%d%d", &C1, &C2);107 Union (S, C1, C2);108 }109scanf"%c", &ch); the }111 thep = head->Next;113TMP =head; the Free(TMP); the while(P) the {117 if(p->flag)118printf"yes\n");119 Else -printf"no\n");121TMP =p;122p = p->Next;123 Free(TMP);124 } theK =0;126 Do127 { - if(S[--maxsize] <0)129k++; the} while(MaxSize); 131 if(k >1) theprintf"there is%d components.\n", k);133 Else134printf"The network is connected.\n");135 //fclose (stdin);//For Test136 return 0;137 }138 139 intFind (intS[],intX) $ {141x--;142 for(; S[X] >=0; X =s[x]);143 144 returnX;145 }146 147 voidUnion (intS[],intX1,intX2)148 {149 intRoot1, Root2; Max 151ROOT1 =Find (S, X1); theRoot2 =Find (S, X2);153 if(Root1! =Root2)154 {155 if(S[root1] <=S[root2])156 {157S[ROOT1] + =S[root2];158S[ROOT2] =Root1;159 } the Else161 {162S[root2] + =S[ROOT1];163S[ROOT1] =Root2;164 }165 }166}
PAT 05-Tree 7 File Transfer