Created
March 1, 2015 13:15
-
-
Save lotz84/9486184725bd7925e704 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import Data.List | |
| type Vertex = Int | |
| type Edge = (Vertex, Vertex) | |
| getEdges :: IO [Edge] | |
| getEdges = do | |
| putStr "> " | |
| line <- getLine | |
| if line == "" | |
| then return [] | |
| else do | |
| let vs = words line | |
| if length vs /= 2 | |
| then do | |
| print "input edge like \"2 5\"" | |
| getEdges | |
| else do | |
| es <- getEdges | |
| return $ (readInt (vs!!0), readInt (vs!!1)) : es | |
| where | |
| readInt :: String -> Int | |
| readInt = read | |
| isSubsetOf :: Eq a => [a] -> [a] -> Bool | |
| isSubsetOf xs ys = and [x `elem` ys | x <- xs] | |
| verticies :: [Edge] -> [Vertex] | |
| verticies es = map head . group . sort . concat $ [[v, w] | (v, w) <- es] | |
| getSource :: [Edge] -> [(Vertex, [Vertex])] | |
| getSource es = [(v, [fst e | e <- es, snd e == v]) | v <- verticies es] | |
| tsort :: [Edge] -> [Vertex] | |
| tsort es = tsort' [] (getSource es) | |
| where | |
| tsort' accum source | |
| | length accum == length source = accum | |
| | otherwise = let novel = [fst s | s <- source, not (fst s `elem` accum), snd s `isSubsetOf` accum] | |
| in tsort' (accum ++ novel) source | |
| main = getEdges >>= print . tsort |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
とりあえず書いてみただけなので効率は悪い
入力例はwikiから