Skip to content

Instantly share code, notes, and snippets.

@paullewallencom
Last active July 27, 2018 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paullewallencom/7ac044ec464b035ff99163a42fe15906 to your computer and use it in GitHub Desktop.
Save paullewallencom/7ac044ec464b035ff99163a42fe15906 to your computer and use it in GitHub Desktop.
C++ Quick-Find Algorithm
#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