見出し画像

Data.{Graph,Tree} を活用する

競プロ典型 90 問 2 日目 - Longest Circular Road(★4)

import Control.Arrow
import Control.Monad
import qualified Data.ByteString.Char8 as C
import Data.Graph
import Data.List
import Data.Tree

main = readLn >>= \n -> sol n <$> replicateM (n-1) get >>= print

get = unfoldr (C.readInt . C.dropWhile (==' ')) <$> C.getLine

sol n as = f . head $ dfs g [v]
  where
  g = buildG (1,n) $ concatMap (\[a,b] -> [(a,b),(b,a)]) as
  v = snd . h . head $ dfs g [1]

-- f :: Tree Int -> Int
f (Node _ []) = 1
f (Node _ ts) = succ . maximum $ map f ts

-- h :: Tree Int -> (Int, Int)
h (Node a []) = (1,a)
h (Node _ ts) = first succ . maximum $ map h ts

いいなと思ったら応援しよう!

karoyakani
ありがとう