Skip to content

Instantly share code, notes, and snippets.

@lawrencechen0921
Created September 16, 2019 15:17
Show Gist options
  • Save lawrencechen0921/c767c59b8b46ecf0d6631418edaef7f0 to your computer and use it in GitHub Desktop.
Save lawrencechen0921/c767c59b8b46ecf0d6631418edaef7f0 to your computer and use it in GitHub Desktop.
Union Find
public static void main(String[ ] args)
{
int N = StdIn. readInt( );
UF uf = new UF(N) ;
while (! StdIn . isEmpty( ) )
{
int p = StdIn . readInt( );
int q = StdIn . readInt( );
if (! uf . connected(p, q) )
{
uf . union (p , q);
StdOut. printIn( p + " " + q);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment