Skip to content

Instantly share code, notes, and snippets.

@ifserge
Created April 12, 2017 15:28
Show Gist options
  • Save ifserge/6d2982b3a63e9002c094035d735eea56 to your computer and use it in GitHub Desktop.
Save ifserge/6d2982b3a63e9002c094035d735eea56 to your computer and use it in GitHub Desktop.
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
parseInt :: String -> Int
parseInt x = read x
solve :: Int -> [(Int,Int)] -> S.Set Int -> Int
solve x [] holes = x
solve x ((a,b):op) holes | S.member x holes = x
| x == a = if (S.member b holes) then b else solve b op holls
| x == b = if (S.member a holes) then a else solve a op holls
| otherwise = solve x op holls
main :: IO ()
main = do
s0 <- getLine
let n = parseInt ((words s0)!!0)
let m = parseInt ((words s0)!!1)
let k = parseInt ((words s0)!!2)
s1 <- getLine
let holes = S.fromList $ map parseInt (words s1)
s3 <- replicateM k getLine
let ops = map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
putStrLn $ show $ solve 1 ops holes
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment