PAT 05 - 트 리 7 파일 전송
19683 단어 File
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
", &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
", &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
");
119 else
120 printf("no
");
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.
", k);
133 else
134 printf("The network is connected.
");
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 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java에서 InputStream, String, File 간의 상호 전환 비교InputStream, String, File 상호 전환 1. String --> InputStream 2. InputStream --> String 오늘 인터넷에서 또 다른 방법을 보았는데, 특별히 가지고 와서 공...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.