* *********************************** Please specify the source: bytes ********************************************
I have designed the Data Structure Course in the past two days. It is not difficult because I have done ACM questions before.
~~~~ Query set: Check the network ~~~~
Question requirements:
Given a list of two-way connections between machines and a computer network, each line allows both computers to directly transfer files. If there is a connection path between other computers, you can also perform indirect file transmission. Write a program to determine whether two computers can transmit files between them.
Input requirements:
The input consists of several test data. For each group of tests, the first line contains an integer n (n ≤ 10000), that is, the total number of computers in the network, therefore, each computer can be expressed by a positive integer between 1 and N. The input format of the following lines is I C1 C2, C C1 C2, or S. C1 and C2 are the serial numbers of the two computers. I indicates entering a line between C1 and C2, C indicates to check whether files can be transmitted between C1 and C2, and s indicates that the test data of this Group ends.
If n is 0, all tests are completed. Do not process the data.
Output requirements:
For tests starting with C in each group, check whether files can be transmitted between C1 and C2. If yes, "yes" is output in one line; otherwise, "no" is output ".
When reading s, check the entire network. If any two computers in the network can transmit files, output "the network is connected. otherwise, the output "There are K components. k indicates the number of connected sets in the network.
Output an empty line to separate the two groups of test data.
Input example:
3
C 1 2
I 1 2
C 1 2
S
3
I 3 1
I 2 3
C 1 2
S
0
Output example:
No
Yes
There are 2 components.
Yes
The network is connected.
This question, well... For typical concurrent queries, you don't need to talk about it much. Then, you can solve the problem by querying the pile of blocks.
What is base?
Of course: -- initialization -- query function -- merge Function
Here I have two methods: one is a direct array, where the serial number exactly represents the machine number, and one array is solved.
The structure is used. Each structure stores a parent node and a rank. The rank is used to merge the set to minimize the tree height.
First, array:
/*************************************** **************************************** * ******** Author: tree ** from: blog.csdn.net/lttree ** title: Check the network ** Source: Data Structure _ Course Design ** hint: and check the set ************************************* **************************************** * **********/# include <iostream> using namespace STD; # define Max ready int par [Max + 2]; // initialization function void Init (int n) {int I; for (I = 1; I <= N; ++ I) par [I] = I;} // The Int find (INT X) {If (x = par [x]) return X; elsereturn (find (par [x]);} // combine the void combine (int x, int y) {int I, j; I = find (X ); j = find (y); par [J] = I;} void main () {// num indicates the total number of machines, and mac1 and mac2 indicates the number of machines to be operated, order is the accepted command int num, mac1, mac2; char order; while (CIN> num) {// exit if (! Num) break; // ### if (Num <1 | num> MAX) {printf ("data out of bounds. Please input it again! \ N "); continue;} // initialize the node. Init (Num); // obtain the command while (scanf ("% C", & Order) {If (order! ='S & order! = 'C' & order! = 'I') {cout <"command error. Please try again! "<Endl; continue;} // The command is s to determine the number of sets and exit the loop if (Order = 's') {// query int I by the number of sets, k = 0; for (I = 1; I <= num; ++ I) if (par [I] = I) ++ K; If (k = 1) cout <"the network is connected. "<Endl; elsecout <" there are % d components. "<Endl; cout <Endl; break;} // If the command is not s, obtain the scanf (" % d ", & mac1, & mac2); // The command is C and determines whether two machines are connected. Otherwise, the command is I, that is, if (Order = 'C ') {// are the two computers in the same group? Int X, Y; X = find (mac1); y = find (mac2); If (x = y) cout <"yes" <Endl; elsecout <"no" <Endl;} else if (Order = 'I') {// connect two computers to combine (mac1, mac2 );}}}}
Second, struct:
/*************************************** **************************************** * ******** Author: tree ** from: blog.csdn.net/lttree ** title: Check the network ** Source: Data Structure _ Course Design ** hint: and check the set ************************************* **************************************** * **********/# include <iostream> using namespace STD; # define Max 10000 // node struct typedef struct node {int data; int rank; int parent;} u Fstree; // initialization function void make_set (ufstree T [], int N) {int I; for (I = 1; I <= N; ++ I) {T [I]. data = I; t [I]. rank = 0; t [I]. parent = I ;}// query the function to query the final parent node int find_set (ufstree T [], int X) {If (X! = T [X]. parent) Return (find_set (t, t [X]. parent); elsereturn (x) ;}// merge the two sets into void Union (ufstree T [], int X, int y) {x = find_set (t, x); y = find_set (T, Y); If (T [X]. rank> T [Y]. rank) T [Y]. parent = x; else {T [X]. parent = y; If (T [X]. rank = T [Y]. rank) T [Y]. rank ++ ;}} void main () {// num is the total number of machines, mac1, mac2 is the number of machines to be operated, order is the accepted command int num, mac1, mac2; char order; ufstree T [Max + 1]; while (CIN> num) {// as required, exit if (! Num) break; // ### if (Num <1 | num> MAX) {cout <"data out of bounds. Please input it again! "<Endl; continue;} // initialize the node. Make_set (T, num); // obtain the command loop while (CIN> order) {// ### whether the command is legal if (order! ='S & order! = 'C' & order! = 'I') {cout <"command error. Please try again! "<Endl; continue;} // The command is s to determine the number of sets and exit the loop if (Order = 's') {// query int I by the number of sets, k = 0; for (I = 1; I <= num; ++ I) if (T [I]. parent = I) + + k; If (k = 1) cout <"the network is connected. "<Endl; elsecout <" there are % d components. "<Endl; cout <Endl; break;} // The command is not S. Obtain the two machine numbers operated: CIN> mac1> mac2; // #### determine whether the machine number is correct while (mac1> num | mac2> num | mac1 <1 | mac2 <1) {printf ("the machine number is incorrect. Please enter it again! \ N "); CIN> mac1> mac2;} // The command is C to determine whether the two machines are connected. Otherwise, the command is I, connect two machines if (Order = 'C') {// are the two computers in the same set? Int X, Y; X = find_set (T, mac1); y = find_set (T, mac2); If (x = y) cout <"yes" <Endl; elsecout <"no" <Endl;} else if (Order = 'I') {// connect two computers to Union (T, mac1, mac2 );}}}}
* *********************************** Please specify the source: bytes ********************************************
Data Structure _ Course Design -- check the set: Check the network