Submission #140394


Source Code Expand

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, ViewPatterns, TupleSections, CPP #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST
import qualified Data.ByteString.Char8 as B
import Data.Array.Base
import Data.Array.ST (runSTUArray)
import Data.List
import Data.STRef
import Data.Function
import GHC.Arr (range, Array)

readInt :: B.ByteString -> Int
readInt bs = case B.readInt bs of Just (n,_) -> n

#if __GLASGOW_HASKELL__ < 706
modifySTRef' :: STRef s a -> (a -> a) -> ST s ()
modifySTRef' r f=do x<-readSTRef r;writeSTRef r$!f x
{-# INLINE modifySTRef' #-}
#endif

rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n f=go 0 where go !i=when(i<n)$f i>>go(i+1)
{-# INLINE rep #-}

main :: IO ()
main = do
  [n1,m1] <- map read.words <$> getLine
  es <- map readInt.B.words <$> B.getContents
  let (es1,n2:m2:es2) = splitAt (m1*2) es
      gr1 = mkGraph (0,n1-1) $ toEdges es1
      gr2 = mkGraph (0,n2-1) $ toEdges es2
      (r1, d1) = solve gr1
      (r2, d2) = solve gr2
  putStrLn . unwords $ map show [maximum[d1,d2,r1+r2+1], d1+d2+1]

solve :: Graph -> (Int, Int)
solve gr = (minimum eccs, maximum eccs)
   where
     !eccs = map (eccentricity gr) $ vertices gr

type Vertex = Int
type Edge = (Vertex, Vertex)
type Graph = Array Vertex (UArray Int Vertex)

toEdges :: [Vertex] -> [Edge]
toEdges (from:to:vs) = (from,to):(to,from):toEdges vs
toEdges _ = []

mkGraph :: (Int, Int) -> [Edge] -> Graph
mkGraph bnd edges = amap toArray gr
   where
     toArray xs = case listArray (0,length xs-1) xs of arr -> arr
     gr :: Array Vertex [Vertex]
     !gr = unsafeAccumArray (flip (:)) [] bnd edges

vertices :: Graph -> [Vertex]
vertices gr = range (bounds gr)

forEachVertexM_ :: (Monad m) => UArray Int Vertex -> (Vertex -> m ()) -> m ()
forEachVertexM_ nexts@(UArray _ _ size _) act = do
  rep size $ act . unsafeAt nexts
{-# INLINE forEachVertexM_ #-}    

eccentricity :: Graph -> Vertex -> Int
eccentricity gr v = foldl'(flip $ max.unsafeAt dist) 0 $ vertices gr
   where
     !dist = bfs gr v

inf :: Int
inf = 0x3f3f3f3f

data Queue s = Q (STRef s [Vertex]) (STRef s [Vertex])

mkQueue :: ST s (Queue s)
mkQueue = Q <$> newSTRef [] <*> newSTRef []

dequeue :: Queue s -> ST s (Maybe Int)
dequeue (Q front rear) = do
  fs <- readSTRef front
  case fs of
    (f:fs') -> writeSTRef front fs' >> return (Just f)
    [] -> do
      rs <- readSTRef rear
      writeSTRef rear []
      case reverse rs of
        (r:rs') -> writeSTRef front rs' >> return (Just r)
        [] -> return Nothing
{-# INLINE dequeue #-}      

enqueue :: Int -> Queue s -> ST s ()
enqueue x (Q _ rear) = modifySTRef' rear (x:)
{-# INLINE enqueue #-}

bfs :: Graph -> Vertex -> UArray Vertex Int
bfs gr start = runSTUArray $ do
  dist <- newArray (bounds gr) inf :: ST s (STUArray s Vertex Int)
  que <- mkQueue
  
  unsafeWrite dist start 0
  enqueue start que
  
  fix $ \loop -> do
    res <- dequeue que
    case res of
      Just v -> do
        dnv <- (1+) <$> unsafeRead dist v
        forEachVertexM_ (unsafeAt gr v) $ \nv -> do
          old <- unsafeRead dist nv
          when (dnv < old) $ do
            unsafeWrite dist nv dnv
            enqueue nv que
        loop
      Nothing -> return ()

  return dist

Submission Info

Submission Time
Task C - 直径
User cojna
Language Haskell (GHC 7.4.1)
Score 100
Code Size 3378 Byte
Status AC
Exec Time 404 ms
Memory 5848 KB

Judge Result

Set Name Small Large
Score / Max Score 50 / 50 50 / 50
Status
AC × 28
AC × 58
Set Name Test Cases
Small 10-small_random-00, 10-small_random-01, 10-small_random-02, 10-small_random-03, 10-small_random-04, 10-small_random-05, 10-small_random-06, 10-small_random-07, 10-small_random-08, 10-small_random-09, 10-small_random-10, 10-small_random-11, 10-small_random-12, 10-small_random-13, 10-small_random-14, 10-small_random-15, 10-small_random-16, 10-small_random-17, 10-small_random-18, 10-small_random-19, 21-small_path-00, 21-small_path-01, 21-small_path-02, 21-small_path-03, 21-small_path-04, 00-sample-00, 00-sample-01, 00-sample-02
Large 00-sample-00, 00-sample-01, 00-sample-02, 10-small_random-00, 10-small_random-01, 10-small_random-02, 10-small_random-03, 10-small_random-04, 10-small_random-05, 10-small_random-06, 10-small_random-07, 10-small_random-08, 10-small_random-09, 10-small_random-10, 10-small_random-11, 10-small_random-12, 10-small_random-13, 10-small_random-14, 10-small_random-15, 10-small_random-16, 10-small_random-17, 10-small_random-18, 10-small_random-19, 20-small_tree-00, 20-small_tree-01, 20-small_tree-02, 20-small_tree-03, 20-small_tree-04, 21-small_path-00, 21-small_path-01, 21-small_path-02, 21-small_path-03, 21-small_path-04, 30-large_random-00, 30-large_random-01, 30-large_random-02, 30-large_random-03, 30-large_random-04, 30-large_random-05, 30-large_random-06, 30-large_random-07, 30-large_random-08, 30-large_random-09, 40-large_comp-00, 40-large_comp-01, 40-large_comp-02, 40-large_comp-03, 40-large_comp-04, 41-large_tree-00, 41-large_tree-01, 41-large_tree-02, 41-large_tree-03, 41-large_tree-04, 42-large_path-00, 42-large_path-01, 42-large_path-02, 42-large_path-03, 42-large_path-04
Case Name Status Exec Time Memory
00-sample-00 AC 95 ms 1244 KB
00-sample-01 AC 31 ms 1236 KB
00-sample-02 AC 24 ms 1312 KB
10-small_random-00 AC 27 ms 1436 KB
10-small_random-01 AC 28 ms 1620 KB
10-small_random-02 AC 26 ms 1244 KB
10-small_random-03 AC 23 ms 1304 KB
10-small_random-04 AC 26 ms 1560 KB
10-small_random-05 AC 25 ms 1628 KB
10-small_random-06 AC 25 ms 1244 KB
10-small_random-07 AC 25 ms 1260 KB
10-small_random-08 AC 24 ms 1240 KB
10-small_random-09 AC 25 ms 1376 KB
10-small_random-10 AC 25 ms 1368 KB
10-small_random-11 AC 23 ms 1432 KB
10-small_random-12 AC 25 ms 1488 KB
10-small_random-13 AC 25 ms 1124 KB
10-small_random-14 AC 37 ms 1624 KB
10-small_random-15 AC 24 ms 1376 KB
10-small_random-16 AC 24 ms 1688 KB
10-small_random-17 AC 26 ms 1732 KB
10-small_random-18 AC 26 ms 1628 KB
10-small_random-19 AC 25 ms 1624 KB
20-small_tree-00 AC 29 ms 1644 KB
20-small_tree-01 AC 25 ms 1184 KB
20-small_tree-02 AC 29 ms 1628 KB
20-small_tree-03 AC 24 ms 1172 KB
20-small_tree-04 AC 206 ms 2516 KB
21-small_path-00 AC 23 ms 1300 KB
21-small_path-01 AC 25 ms 1300 KB
21-small_path-02 AC 26 ms 1240 KB
21-small_path-03 AC 26 ms 1304 KB
21-small_path-04 AC 25 ms 1428 KB
30-large_random-00 AC 58 ms 4188 KB
30-large_random-01 AC 25 ms 1500 KB
30-large_random-02 AC 29 ms 1884 KB
30-large_random-03 AC 24 ms 1308 KB
30-large_random-04 AC 48 ms 2388 KB
30-large_random-05 AC 25 ms 1372 KB
30-large_random-06 AC 34 ms 1884 KB
30-large_random-07 AC 25 ms 1688 KB
30-large_random-08 AC 404 ms 5848 KB
30-large_random-09 AC 402 ms 5848 KB
40-large_comp-00 AC 24 ms 1300 KB
40-large_comp-01 AC 27 ms 1880 KB
40-large_comp-02 AC 26 ms 1648 KB
40-large_comp-03 AC 29 ms 2520 KB
40-large_comp-04 AC 42 ms 3292 KB
41-large_tree-00 AC 24 ms 1244 KB
41-large_tree-01 AC 27 ms 1696 KB
41-large_tree-02 AC 28 ms 1748 KB
41-large_tree-03 AC 52 ms 1884 KB
41-large_tree-04 AC 198 ms 2528 KB
42-large_path-00 AC 34 ms 1824 KB
42-large_path-01 AC 23 ms 1436 KB
42-large_path-02 AC 25 ms 1624 KB
42-large_path-03 AC 80 ms 2080 KB
42-large_path-04 AC 151 ms 2520 KB