04-Tree 5. File Transfer (25) time limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen, Yue
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?
Input Specification:
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:
I C1 C2
Where I stands for inputting a connection between C1 and C2; Or
C C1 C2
Where C stands for checking if it's possible to transfer files between C1 and C2; Or
S
Where S stands for stopping the this case.
Output Specification:
For each C case, print in one line the word "yes" or "no" if it's possible or impossible to transfer files Betwe En 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:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5S
Sample Output 1:
Nonoyesthere is 2 components.
Sample Input 2:
5C 3 2I 3 2C 1 5I 4 5I 2 4C 3 5I 1 3C 1 5S
Sample Output 2:
Nonoyesyesthe Network is connected.
#include <stdio.h> #include <stdlib.h>int findset (int *set, int val) {//returns the root node of the tree while (Set[val] >= 0) {val = Set[val];} return Val;} void Unionset (int *set, int c1, int c2) {//by size and int root1 = Findset (set, C1); int root2 = Findset (set, C2); if (Set[root1] & Lt Set[root2]) {//tree 1 Larger, connect tree 2 to tree 1 root node set[root1] + = Set[root2];set[root2] = root1;} else {Set[root2] + = set[root1];set[root1] = Root2;}} int main () {//freopen ("test.txt", "R", stdin), int n;scanf ("%d", &n), int *set = (int *) malloc (sizeof (int) * (n + 1)); fo R (int i = 0; I <= N; ++i) {//initialization tree size, root node negative value, absolute value indicates tree size set[i] = 1;} Char ch;scanf ("\n%c", &ch), while (ch! = ' S ') {int C1, C2;SCANF ("%d%d", &c1, &C2), if (ch = = ' I ') {Unionset (set, C1, C2);//c1,c2 where the set is asked and}else if (ch = = ' C ') {if (Findset (set, C1) = = Findset (set, C2)) {printf ("yes\n");} else {printf ("no\n");}} scanf ("\n%c", &ch);} int cnt = 0;for (int i = 1; I <= n; ++i) {//Compute root node count if (Set[i] < 0) {++cnt;}} if (cnt = = 1) {printf ("The Network is Connected.\n");} ElSE {printf ("There is%d components.\n", CNT);} Free (set); return 0;}
Title Link: http://www.patest.cn/contests/mooc-ds/04-%E6%A0%915
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
04-Tree 5. File Transfer (25)