Skip to content

Instantly share code, notes, and snippets.

@tanakh
Created January 30, 2011 08:11
Show Gist options
  • Save tanakh/802680 to your computer and use it in GitHub Desktop.
Save tanakh/802680 to your computer and use it in GitHub Desktop.
import Data.Vector.Primitive.Mutable
import Control.Monad
import Prelude hiding (read)
newUF :: Int -> IO (IOVector Int)
newUF n = do
v <- new n
forM_ [0..n-1] $ \i -> write v i i
return v
find a v = do
b <- read v a
if a==b
then return a
else do
c <- find b v
write v a c
return c
unite a b v = do
x <- find a v
y <- find b v
write v x y
main :: IO ()
main = do
uf <- newUF 10
unite 1 2 uf
unite 2 3 uf
print =<< find 1 uf
print =<< find 2 uf
print =<< find 3 uf
print =<< find 4 uf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment