The algorithm is based on an integer array, when p and Q are connected only if the p and Q elements are equal. initially, the value of element I in the array is i,0<=i<n, in order to implement the merging of P and Q, we iterate over the array and change the value of all elements named P to Q. We can also choose another way to change all the elements named Q to P.
The program reads a non-negative integer pair sequence that is less than n from the standard input (to P-q, "connect the object Beta to Q"), and the output is not connected to the input pair. An array ID is used in the program, each element represents an object and has the following properties, when and only when P and Q are connected, id[p] and id[q] want to wait. For simplicity, define n as a compile-time constant. On the other hand, you can also get it from the input and dynamically assign it an array of IDs.
/*@file Quickfind.c@brief uses fast lookup algorithms to solve small-scale connectivity problems*/#include<stdio.h>#defineN 1000intMainvoid){ intI, p, Q, t; intId[n]; //Initializes the initial values of the elements in the collection of objects for(i =0; i < N; i++) Id[i] =i; //loop read-in integer pairs while(scanf_s ("%d-%d", &p, &q) = =2) { //if the object p is connected to Q, the next pair of integers is read from the standard input if(Id[p] = = Id[q])Continue; //if the values of id[p] and id[q] are not equal, then P-Q is the new pair//All elements that are equal to the value of the id[p] element are connected to the Q for(t = id[p], i =0; i < N; i++) { if(Id[i] = =t) id[i]=Id[q]; } //because P-q is new, so output this toprintf"New pair:%d-%d\n", p, q); } return 0;}
I don't know what this is for, it's just a traversal, a value assignment.
"Algorithm C language Implementation" ———— fast-Find algorithm (Quick-find algorithm)