-
Notifications
You must be signed in to change notification settings - Fork 0
/
even_tree.hs
55 lines (44 loc) · 1.85 KB
/
even_tree.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Data.Functor
import Control.Applicative
import Data.Maybe
import Debug.Trace
import qualified Data.Set as S
import qualified Data.Map as M
data TNode = TNode { tn_id :: Int
, tn_subnodes :: [TNode] }
deriving (Show, Eq)
tn_build :: Int -> [(Int, Int)] -> TNode
tn_build n edges =
let joints = foldl (\acc (ll, rr) ->
let ins a l r =
case M.lookup l a of
Nothing -> M.insert l [r] a
Just redges -> M.insert l (r:redges) a
in ins (ins acc ll rr) rr ll)
M.empty edges
subbuild_node nid =
TNode nid (map subbuild_node (filter (\nnid -> nnid > nid) (fromJust $ M.lookup nid joints)))
in subbuild_node 1
opt_balance :: Bool -> [(Maybe Int, Maybe Int)] -> Maybe Int
opt_balance e [] = if e then Just 0 else Nothing
opt_balance e (x:xs) =
let cases = [ (+) <$> fst x <*> (opt_balance (not e) xs)
, (+) <$> snd x <*> opt_balance e xs ]
succ_cases = map fromJust (filter isJust cases)
in if succ_cases == []
then Nothing
else Just $ maximum succ_cases
solve :: TNode -> Int
solve tn =
let solve_for :: TNode -> Bool -> Maybe Int
solve_for tn p =
let
subsolutions = map (\sn -> ( solve_for sn True
, (+) 1 <$> solve_for sn False))
(tn_subnodes tn)
in opt_balance p subsolutions
in fromJust $ solve_for tn False
main = do
[n, m] <- map read <$> words <$> getLine :: IO [Int]
edges <- mapM (\_ -> (\[l, r] -> (l, r)) <$> map read <$> words <$> getLine) [1..m] :: IO [(Int, Int)]
putStrLn $ show $ solve $ tn_build n edges