Last active
July 27, 2018 00:33
-
-
Save paullewallencom/7ac044ec464b035ff99163a42fe15906 to your computer and use it in GitHub Desktop.
C++ Quick-Find Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define N 10000 | |
main() | |
{ | |
int i, p, q, t, id[ N ]; | |
for ( i = 0; i < N; i++ ) id[ i ] = i; | |
while ( scanf( "%d %d\n", &p, &q ) == 2 ) | |
{ | |
if ( id[ p ] == id[ q ] ) continue; | |
for ( t = id[ p ], i = 0; i < N; i++ ) | |
if ( id[ i ] == t ) id[ i ] = id[ q ]; | |
printf( " %d %d\n", p, q ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment