Submission #56123528


Source Code Expand

Copy
{-# LANGUAGE
ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
NoMonomorphismRestriction, GADTs, PatternGuards, MagicHash,
UnboxedTuples, InstanceSigs, DataKinds, TypeOperators,
RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns, PolyKinds,
TypeFamilies, OverloadedStrings, FlexibleInstances, UndecidableInstances,
DefaultSignatures, GeneralizedNewtypeDeriving, StandaloneDeriving,
DeriveGeneric, DeriveFunctor, DeriveDataTypeable, DeriveFoldable,
DeriveTraversable, DeriveDataTypeable, FlexibleInstances,
MultiParamTypeClasses, TypeApplications, RecordWildCards,
PackageImports, DerivingStrategies, PatternSynonyms,
NumericUnderscores, ConstraintKinds #-}
{-# OPTIONS_GHC -O2 -Wno-unused-top-binds -Wno-unused-imports -Wno-orphans #-}
#include "MachDeps.h"
#define PHASE_FUSED [1]
#define PHASE_INNER [0]
#define INLINE_FUSED INLINE PHASE_FUSED
#define INLINE_INNER INLINE PHASE_INNER
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
{-# LANGUAGE
  ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
  LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
  NoMonomorphismRestriction, GADTs, PatternGuards, MagicHash,
  UnboxedTuples, InstanceSigs, DataKinds, TypeOperators,
  RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns, PolyKinds,
  TypeFamilies, OverloadedStrings, FlexibleInstances, UndecidableInstances,
  DefaultSignatures, GeneralizedNewtypeDeriving, StandaloneDeriving,
  DeriveGeneric, DeriveFunctor, DeriveDataTypeable, DeriveFoldable,
  DeriveTraversable, DeriveDataTypeable, FlexibleInstances,
  MultiParamTypeClasses, TypeApplications, RecordWildCards,
  PackageImports, DerivingStrategies, PatternSynonyms,
  NumericUnderscores, ConstraintKinds #-}
{-# OPTIONS_GHC -O2 -Wno-unused-top-binds -Wno-unused-imports -Wno-orphans #-}

#include "MachDeps.h"

#define PHASE_FUSED [1]
#define PHASE_INNER [0]
#define INLINE_FUSED INLINE PHASE_FUSED
#define INLINE_INNER INLINE PHASE_INNER

import Prelude
import Data.Bits
import Data.List as Lst
import Data.Maybe
import Data.Tuple
import Data.Ord
import Data.Int
import Data.Word
import Data.Char
import Data.Ratio
import Data.Function
import Data.STRef
import Data.IORef
import Data.Monoid
import Data.Functor
import Data.Functor.Identity
import Data.Data
import Data.Typeable
import Data.Bifunctor
import Data.Foldable as Fld
import Data.Traversable as Tr
import GHC.Generics
import System.IO
import System.IO.Unsafe (unsafeDupablePerformIO, unsafePerformIO)
import qualified Control.Arrow as Aw
import Control.Applicative
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.State.Strict
import Control.Monad.ST
import Control.Monad.ST.Lazy (strictToLazyST, lazyToStrictST)
import qualified Control.Monad.ST.Lazy as STL
-- import Control.Monad.ST.Safe
import Control.DeepSeq
import Data.Coerce
import Data.Primitive.MutVar
import qualified Data.Primitive as Prim
import qualified Data.ByteString as BSW
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy as BSLW
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Unsafe as BSU
import qualified Data.ByteString.Internal as BSU
import qualified Data.IntPSQ as IntPSQ
import Data.IntPSQ (IntPSQ)
import qualified Data.OrdPSQ as OrdPSQ
import Data.OrdPSQ (OrdPSQ)
import qualified Data.HashPSQ as HashPSQ
import Data.HashPSQ (HashPSQ)
import qualified Data.HashMap.Strict as HashMap
import qualified Data.HashMap.Strict as HMS
import qualified Data.HashMap.Lazy as HashMapL
import qualified Data.HashMap.Lazy as HML
import Data.HashMap.Strict (HashMap)
import qualified Data.IntMap.Strict as IntMap
import qualified Data.IntMap.Strict as IMS
import qualified Data.IntMap.Lazy as IntMapL
import qualified Data.IntMap.Lazy as IML
import Data.IntMap (IntMap)
import qualified Data.Map.Strict as Map
import qualified Data.Map.Lazy as MapL
import Data.Map.Strict (Map)
import Data.List as List
import Data.Hashable (Hashable)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.HashSet (HashSet)
import qualified Data.HashSet as HashSet
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import qualified Data.IntSet as IS
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Storable.Mutable as VSM
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Primitive.Mutable as VPM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector.Generic.New as VGN
import qualified Data.Vector.Fusion.Bundle.Monadic as VFBM
import qualified Data.Vector.Fusion.Bundle as VFB
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Fusion.Bundle.Size as VFBS
import qualified Data.Vector.Fusion.Util as VFU
import qualified Data.Vector.Algorithms.Intro as VAIT
import qualified Data.Attoparsec.ByteString.Char8 as Atto
import qualified Debug.Trace
import qualified Data.Mutable as DMut
import Unsafe.Coerce
import Foreign.ForeignPtr
import GHC.Float (castWord64ToDouble, castDoubleToWord64)
import GHC.Word (Word(..), Word64(..))
import GHC.Exts (build, Int(..),
                 (+#), (*#), (-#), (<#), (>=#), (==#), quotRemInt#,
                 remInt#, uncheckedIShiftRL#, andI#, orI#,
                 Addr#, Ptr(..),
                 timesWord2#, quotRemWord2#, minusWord#, Word#,
                 uncheckedShiftRL#, plusWord#, and#, word2Int#,
                 isTrue#, Int#, not#, negateInt#, int2Word#, ltWord#,
                 eqWord#, neWord#, or#, ctz#, timesWord#, leWord#,
                 uncheckedShiftL#, plusWord2#, geWord# )
import GHC.TypeLits ( type (<=) )
import GHC.TypeNats ( KnownNat, Nat )
import qualified GHC.TypeNats as TNats ( natVal )
import Numeric.Natural
import Data.Reflection
import Math.NumberTheory.Logarithms (naturalLog2)
import GHC.Stack
import GHC.Int
import qualified Data.Heap as Heap
import Data.Heap (Heap, Entry(..))
import qualified Data.Massiv.Array as M
import qualified Data.Massiv.Array.Unsafe as M
import Data.Massiv.Array (Ix2(..), Sz2, pattern Sz2)
import qualified Data.Massiv.Array.Manifest.Vector as M
#if __GLASGOW_HASKELL__ >= 902
import GHC.Num.Integer
#endif
{-# RULES "Force inline VAIT.sort" VAIT.sort = VAIT.sortBy compare #-}

inf :: Int
inf = maxBound `div` 2

data PHNode = PHNil | PHNode {
  phKey :: {-# UNPACK #-} !Int,
  phVal :: {-# UNPACK #-} !Int,
  phSibling :: PHNode,
  phChild :: PHNode
}

-- Invariant: if phRoot == PHNode{ pHSibling, ..} then pHSibling == PHNil
newtype PairingHeap = PairingHeap { phRoot :: PHNode }

phMeld :: PHNode -> PHNode -> PairingHeap
phMeld x@(PHNode xk _ _ xc) y@(PHNode yk _ _ yc)
  | xk <= yk = PairingHeap $ x { phSibling = PHNil, phChild = y { phSibling = xc } }
  | otherwise = PairingHeap $ y { phSibling = PHNil, phChild = x { phSibling = yc } }
phMeld PHNil PHNil = PairingHeap PHNil
phMeld PHNil y = PairingHeap $ y { phSibling = PHNil }
phMeld x PHNil = PairingHeap $ x { phSibling = PHNil }

phMeldList :: PHNode -> PairingHeap
phMeldList x@(PHNode _ _ y@(PHNode _ _ xs _) _) = phMeld (phRoot $ phMeld x y) $ phRoot $ phMeldList xs
phMeldList x = PairingHeap x

phSing :: Int -> Int -> PairingHeap
phSing k v = PairingHeap $ PHNode k v PHNil PHNil

phIns :: Int -> Int -> PairingHeap -> PairingHeap
phIns k v (PairingHeap h) = phMeld (PHNode k v PHNil PHNil) h

phMin :: PairingHeap -> Maybe (Int, Int, PairingHeap)
phMin (PairingHeap PHNil) = Nothing
phMin (PairingHeap (PHNode k v _ c)) = Just (k, v, phMeldList c)

phEmpty :: PairingHeap
phEmpty = PairingHeap PHNil


data FixedCapBHeap s = FixedCapBHeap {
  fbhLen :: {-# UNPACK #-} !(DMut.PRef s Int),
  fbhArr :: {-# UNPACK #-} !(VUM.MVector s (Int,Int))
}

fcbhUpHeap :: PrimMonad m => FixedCapBHeap (PrimState m) -> Int -> m ()
fcbhUpHeap (FixedCapBHeap _ arr) !i0 = stToPrim $ do
  (!ak, !av) <- VUM.unsafeRead arr (i0-1)
  let go !i
        | i <= 1 = VUM.unsafeWrite arr 0 (ak,av)
        | otherwise = do
            let !ip = i .>>. 1
            (!pk, !pv) <- VUM.unsafeRead arr (ip-1)
            if ak < pk
              then VUM.unsafeWrite arr (i-1) (pk,pv) >> go ip
              else VUM.unsafeWrite arr (i-1) (ak,av)
  go i0

fcbhDownHeap :: PrimMonad m => FixedCapBHeap (PrimState m) -> Int -> m ()
fcbhDownHeap (FixedCapBHeap len arr) !i0 = stToPrim $ do
  !len <- DMut.readRef len
  let !hasChild = len .>>. 1
  (!ak, !av) <- VUM.unsafeRead arr (i0-1)
  let go !i
        | i > hasChild = VUM.unsafeWrite arr (i-1) (ak,av)
        | otherwise = do
            let !lc = i .<<. 1
                !rc = lc + 1
            (!lk, !lv) <- VUM.unsafeRead arr (lc-1)
            if rc <= len
              then do
                (!rk, !rv) <- VUM.unsafeRead arr (rc-1)
                if lk < rk
                  then if ak < lk
                    then VUM.unsafeWrite arr (i-1) (ak,av)
                    else VUM.unsafeWrite arr (i-1) (lk,lv) >> go lc
                  else if ak < rk
                    then VUM.unsafeWrite arr (i-1) (ak,av)
                    else VUM.unsafeWrite arr (i-1) (rk,rv) >> go rc
              else if ak < lk
                then VUM.unsafeWrite arr (i-1) (ak,av)
                else VUM.unsafeWrite arr (i-1) (lk,lv) >> go lc
  go i0

fcbhFullHeapify :: PrimMonad m => FixedCapBHeap (PrimState m) -> m ()
fcbhFullHeapify hp@(FixedCapBHeap len arr) = stToPrim $ do
  !len <- DMut.readRef len
  let !half = len .>>. 1
      go i | i > 0 = fcbhDownHeap hp i >> go (i-1)
           | otherwise = return ()
  go half

-- | O(1). Push an element into the heap without guaranteeing the heap property.
-- This is useful when you call @fcbhFullHeapify@ afterwards;
-- note that @fcbhFullHeapify@ is O(n) and @fcphPushWithoutHeapify@ is O(1),
-- while @fcbhPush@ n times is O(n log n).
fcbhPushWithoutHeapify :: PrimMonad m => FixedCapBHeap (PrimState m) -> Int -> Int -> m ()
fcbhPushWithoutHeapify (FixedCapBHeap lenStore arr) !k !v = stToPrim $ do
  !len <- DMut.readRef lenStore
  when (len >= VUM.length arr) $ error "FixedCapBHeap: push: overflow"
  VUM.unsafeWrite arr len (k,v)
  DMut.writeRef lenStore $! len+1

-- | O(log n). Push an element into the heap.
fcbhPush :: PrimMonad m => FixedCapBHeap (PrimState m) -> Int -> Int -> m ()
fcbhPush hp@(FixedCapBHeap lenStore arr) !k !v = stToPrim $ do
  !len <- DMut.readRef lenStore
  when (len >= VUM.length arr) $ error "FixedCapBHeap: push: overflow"
  VUM.unsafeWrite arr len (k,v)
  DMut.writeRef lenStore $! len+1
  fcbhUpHeap hp $! len+1

-- | O(log n). Pop the minimum element from the heap.
fcbhPop :: PrimMonad m => FixedCapBHeap (PrimState m) -> m (Maybe (Int, Int))
fcbhPop hp@(FixedCapBHeap lenStore arr) = stToPrim $ do
  !len <- DMut.readRef lenStore
  if len <= 0 then return Nothing else do
    !minv <- VUM.unsafeRead arr 0
    VUM.unsafeWrite arr 0 =<< VUM.unsafeRead arr (len-1)
    DMut.writeRef lenStore $! len-1
    when (len > 1) $ fcbhDownHeap hp 1
    return $ Just minv

-- | O(1). Clear the heap.
fcbhClear :: PrimMonad m => FixedCapBHeap (PrimState m) -> m ()
fcbhClear (FixedCapBHeap lenStore _) = DMut.writeRef lenStore 0

-- | Create a new fixed-capacity binary heap.
fcbhNew :: PrimMonad m => Int -> m (FixedCapBHeap (PrimState m))
fcbhNew cap = FixedCapBHeap <$> DMut.newRef 0 <*> VUM.new cap

-- updateWithDijk with a fixed-capacity binary heap.
updateWithDijkFcbh :: PrimMonad m
  => GraphData Int -- ^ Graph data
  -> FixedCapBHeap (PrimState m) -- ^ pre-allocated heap; this function will taint it
  -> M.MVector (PrimState m) M.U Int -- ^ distance array
  -> m ()
updateWithDijkFcbh grph heap store = stToPrim $ do
  fcbhClear heap
  M.iforPrimM_ store $ \ !i !di -> do
    when (di < inf) $ fcbhPushWithoutHeapify heap di i
  fcbhFullHeapify heap
  go $ M.sizeOfMArray store
  where
    go !n | n <= 0 = return ()
    go !n = do
      !minv <- fcbhPop heap
      case minv of
        Nothing -> return ()
        Just (!dv, !v) -> do
          curv <- M.unsafeRead store v
          if dv > curv then go n else do
            VU.forM_ (edgesOut grph v) $ \(w,dvw) -> do
              !curw <- M.readM store w
              let !dw = dv + dvw
              when (dw < curw) $ do
                M.unsafeWrite store w dw
                fcbhPush heap dw w
            go $! n-1

dpFcbh :: Int -> Int -> GraphData Int -> M.Array M.U Ix2 Int
dpFcbh n k grph@(GraphData elist _) = M.withLoadMArrayST_ 
  (M.makeArrayR M.D M.Seq (Sz2 (bit k) n) 
  $ \ (i :. j) -> if j < k && i == bit j then 0 else inf) $ \ store -> do
    let m = shiftR (VU.length elist) 1
    heap <- fcbhNew (n+m)
    VU.forM_ (VU.generate (bit k - 1) (+1)) $ \ !pat -> do
      let !topErased = clearBit pat $ finiteBitSize pat - countLeadingZeros pat - 1
      mapM_ (\ !pat' -> sumMinRow store pat' (pat .&. complement pat') pat)
        $ takeWhile (> 0) $ iterate (\ !x -> (x-1) .&. pat) topErased
      spat <- M.outerSliceMArrayM store pat
      updateWithDijkFcbh grph heap spat
      traceShowM =<< M.freezeS spat

updateWithDijk :: PrimMonad m => GraphData Int -> M.MVector (PrimState m) M.U Int -> m ()
updateWithDijk grph store = stToPrim $ do
  !initHeap <- (`execStateT` phEmpty)
    $ M.iforPrimM_ store $ \ !i !di -> modify' $ \ !heap ->
    if di >= inf then heap else phIns di i heap
  go (M.sizeOfMArray store) initHeap
  where
    go !n !_ | n <= 0 = return ()
    go !n !(phMin -> Just (!dv, !v, !rest)) = do
      curv <- M.unsafeRead store v
      if curv < dv then go n rest else do -- dv == curv already; no need to overwrite again
        !rest <- (\f -> VU.foldM' f rest $ edgesOut grph v) $ \ !heap (!w,!dvw) -> do
          !curw <- M.readM store w
          let !dw = dv + dvw
          if dw >= curw then return heap else do
            M.unsafeWrite store w dw
            return $! phIns dw w heap
        go (n-1) rest
    go !_ !_ = return ()

sumMinRow :: PrimMonad m => M.MArray (PrimState m) M.U Ix2 Int -> Int -> Int -> Int -> m ()
sumMinRow mat !src1 !src2 !dst = stToPrim $ do
  !src1 <- M.outerSliceMArrayM mat src1
  !src2 <- M.outerSliceMArrayM mat src2
  !dst <- M.outerSliceMArrayM mat dst
  M.ifor2PrimM_ src1 src2 $ \ !i !a !b -> void $ M.unsafeModify dst (\ !x -> return $! min x (a+b)) i

dp :: Int -> Int -> GraphData Int -> M.Array M.U Ix2 Int
dp n k grph = M.withLoadMArrayST_ 
  (M.makeArrayR M.D M.Seq (Sz2 (bit k) n) 
  $ \ (i :. j) -> if j < k && i == bit j then 0 else inf) $ \ store ->
  VU.forM_ (VU.generate (bit k - 1) (+1)) $ \ !pat -> do
    let !topErased = clearBit pat $ finiteBitSize pat - countLeadingZeros pat - 1
    mapM_ (\ !pat' -> sumMinRow store pat' (pat .&. complement pat') pat)
      $ takeWhile (> 0) $ iterate (\ !x -> (x-1) .&. pat) topErased
    updateWithDijk grph =<< M.outerSliceMArrayM store pat

solve2 :: Int -> Int -> GraphData Int -> VU.Vector Int
solve2 n k grph = M.toVector $ M.drop (M.Sz1 (k-1)) $ dpFcbh n (k-1) grph M.!> (bit (k-1) - 1)

solve :: Int -> Int -> GraphData Int -> VU.Vector Int
solve n k grph = M.toVector $ M.drop (M.Sz1 (k-1)) $ dp n k grph M.!> (bit k - 1)




main :: IO ()
main = do
  [n,m,k] <- map readInt . words <$> getLine
  grph <- getUndirectedGraph n m $ readLnWith $ liftA3 (,,) rInt1 rInt1 rInt
  printVecInLines $ solve2 n k grph
  return ()

debug :: Bool
debug = False

type Vertex = Int
data GraphData d = GraphData {-# UNPACK #-} !(VU.Vector (Vertex,d))
                             {-# UNPACK #-} !(VU.Vector Int)
data MGraphData s d = MGraphData {-# UNPACK #-} !(VUM.MVector s (Vertex,d))
                                 {-# UNPACK #-} !(VU.Vector Int)
data TreeData d = TreeData {-# UNPACK #-} !Vertex
                           {-# UNPACK #-} !(GraphData d)



parentInTree :: (VU.Unbox d)
  => TreeData d -> Vertex -> Maybe (Vertex,d)
parentInTree (TreeData root (GraphData connTable degCount)) vert
  | vert == root = Nothing
  | otherwise    = Just $! connTable VU.! (degCount VU.! vert)

childrenInTree :: (VU.Unbox d)
  => TreeData d -> Vertex -> VU.Vector (Vertex,d)
childrenInTree (TreeData root (GraphData connTable degCount)) vert
  = VU.slice beg (end-beg) connTable
  where
    !end = degCount VU.! (vert+1)
    !beg | vert == root = degCount VU.! vert
         | otherwise    = 1 + degCount VU.! vert

edgesOut :: (VU.Unbox d)
  => GraphData d -> Vertex -> VU.Vector (Vertex,d)
edgesOut (GraphData connTable degCount) vert
  = VU.slice beg (end-beg) connTable
  where
    !end = degCount VU.! (vert+1)
    !beg = degCount VU.! vert

{-# INLINE getTree #-}
getTree :: (PrimMonad m, VU.Unbox d)
  => Int -> Vertex -> m (Int,Int,d) -> m (TreeData d)
getTree !numVert !root !getEdge = do
  !gd <- getUndirectedGraphMut numVert (numVert-1) getEdge
  dfsFindTree gd root
  !gd <- unsafeFreezeGraphData gd
  return $ TreeData root gd 

type Depth = Int
dfsFindTreeAndDepthInMGraph :: (PrimMonad m, VU.Unbox d)
  => MGraphData (PrimState m) d -> Vertex -> m (VU.Vector Depth)
dfsFindTreeAndDepthInMGraph (MGraphData connTable degCount) !root = do
  depths <- VUM.replicate (VU.length degCount - 1) (-1)
  let dfs !parent !this !depth = do
        !prevDepth <- VUM.read depths this
        when (prevDepth < 0) $ do
          VUM.unsafeWrite depths this depth
          let !beg = VU.unsafeIndex degCount this
              !end = VU.unsafeIndex degCount (this+1) - 1
          forM_ [beg..end] $ \ !i -> do
            (!vertex,!d) <- VUM.read connTable i
            if vertex == parent
              then do !vbeg <- VUM.unsafeRead connTable beg
                      VUM.unsafeWrite connTable beg (vertex,d)
                      VUM.unsafeWrite connTable i vbeg
              else dfs this vertex (depth+1)
  dfs (-1) root 0
  VU.unsafeFreeze depths

dfsFindTree :: (PrimMonad m, VU.Unbox d)
  => MGraphData (PrimState m) d -> Vertex -> m ()
dfsFindTree (MGraphData connTable degCount) !root = do
  vis <- VUM.replicate (VU.length degCount - 1) False
  let dfs !parent !this = do
        !vised <- VUM.read vis this
        unless vised $ do
          VUM.unsafeWrite vis this True
          let !beg = VU.unsafeIndex degCount this
              !end = VU.unsafeIndex degCount (this+1) - 1
          forM_ [beg..end] $ \ !i -> do
            (!vertex,!d) <- VUM.read connTable i
            if vertex == parent
              then do !vbeg <- VUM.unsafeRead connTable beg
                      VUM.unsafeWrite connTable beg (vertex,d)
                      VUM.unsafeWrite connTable i vbeg
              else dfs this vertex
  dfs (-1) root

{-# INLINE unsafeFreezeGraphData #-}
unsafeFreezeGraphData :: (PrimMonad m, VU.Unbox d) =>
  MGraphData (PrimState m) d -> m (GraphData d)
unsafeFreezeGraphData (MGraphData connTable degCount)
  = (`GraphData` degCount) <$!> VU.unsafeFreeze connTable 

{-# INLINE getDirectedGraphBidir #-}
getDirectedGraphBidir :: (PrimMonad m, VU.Unbox d)
  => Int -> Int -> (m (Int,Int,d))
  -> m (GraphData d, GraphData d)
getDirectedGraphBidir !numVert !numEdges getEdge = do
  (!forward,!backward) <- getDirectedGraphMutBidir numVert numEdges getEdge
  !forwardImmut <- unsafeFreezeGraphData forward
  !backwardImmut <- unsafeFreezeGraphData backward
  return $! (forwardImmut, backwardImmut)

{-# INLINE getDirectedGraphForward #-}
getDirectedGraphForward :: (PrimMonad m, VU.Unbox d) =>
  Int -> Int -> m (Int,Int,d) -> m (GraphData d)
getDirectedGraphForward !numVert !numEdges getEdge = do
  getDirectedGraphMutForward numVert numEdges getEdge >>= unsafeFreezeGraphData


{-# INLINE getUndirectedGraph #-}
getUndirectedGraph
  :: (PrimMonad m, VU.Unbox d) =>
  Int -> Int -> (m (Int,Int,d)) -> m (GraphData d)
getUndirectedGraph !numVert !numEdges getEdge
  = getUndirectedGraphMut numVert numEdges getEdge >>= unsafeFreezeGraphData

{-# INLINE getUndirectedGraphMut #-}
getUndirectedGraphMut
  :: (PrimMonad m, VU.Unbox d)
  => Int -> Int -> (m (Int,Int,d)) -> m (MGraphData (PrimState m) d)
getUndirectedGraphMut !numVert !numEdges getEdge = do
  !degCount <- VUM.replicate (numVert+1) 0
  !connTable <- VUM.new (shiftL numEdges 1)
  write degCount connTable
  MGraphData connTable <$!> VU.unsafeFreeze degCount
  where
    write !degCount !connTable = do
      !temp <- do
        !temp <- VUM.new numEdges
        VU.forM_ (VU.generate numEdges id) $ \ !i -> do
          (!a,!b,!d) <- getEdge
          VUM.unsafeWrite temp i (a,b,d)
          VUM.modify degCount (+1) (a+1)
          VUM.modify degCount (+1) (b+1)
        VU.unsafeFreeze temp
      VU.foldM'
        (\ !total !i -> do
            !di <- VUM.unsafeRead degCount i
            VUM.unsafeWrite degCount i total
            return $! total+di)
        0 (VU.generate (numVert+1) id)
      VU.forM_ temp $ \(!a,!b,!d) -> do
        !ia <- VUM.read degCount (a+1)
        VUM.unsafeWrite degCount (a+1) $! ia+1
        VUM.unsafeWrite connTable ia $! (b,d)
        !ib <- VUM.read degCount (b+1)
        VUM.unsafeWrite degCount (b+1) $! ib+1
        VUM.unsafeWrite connTable ib $! (a,d)
      
getDirectedGraphMutForward :: (PrimMonad m, VU.Unbox d)
  => Int -> Int -> (m (Int,Int,d))
  -> m (MGraphData (PrimState m) d)
getDirectedGraphMutForward !numVert !numEdges getEdge = do
  !degCount <- VUM.replicate (numVert+1) 0
  !connTable <- VUM.new numEdges
  write degCount connTable
  MGraphData connTable <$!> VU.unsafeFreeze degCount
  where
    write !degCount !connTable = do
      !temp <- do
        !temp <- VUM.new numEdges
        VU.forM_ (VU.generate numEdges id) $ \ !i -> do
          (!a,!b,!d) <- getEdge
          VUM.unsafeWrite temp i (a,b,d)
          VUM.modify degCount (+1) (a+1)
        VU.unsafeFreeze temp
      VU.foldM'
        (\ !total !i -> do
            !di <- VUM.unsafeRead degCount i
            VUM.unsafeWrite degCount i total
            return $! total+di)
        0 (VU.generate (numVert+1) id)
      VU.forM_ temp $ \(!a,!b,!d) -> do
        !ia <- VUM.read degCount (a+1)
        VUM.unsafeWrite degCount (a+1) $! ia+1
        VUM.unsafeWrite connTable ia $! (b,d)

getDirectedGraphMutBidir :: (PrimMonad m, VU.Unbox d)
  => Int -> Int -> (m (Int,Int,d))
  -> m (MGraphData (PrimState m) d, MGraphData (PrimState m) d)
getDirectedGraphMutBidir !numVert !numEdges getEdge = do
  !fDegCount <- VUM.replicate (numVert+1) 0
  !fConnTable <- VUM.new numEdges
  !bDegCount <- VUM.replicate (numVert+1) 0
  !bConnTable <- VUM.new numEdges
  write fDegCount fConnTable bDegCount bConnTable
  !forward <- MGraphData fConnTable <$> VU.unsafeFreeze fDegCount
  !backward <- MGraphData bConnTable <$> VU.unsafeFreeze bDegCount
  return $! (forward,backward)
  where
    write !fDegCount !fConnTable !bDegCount !bConnTable = do
      !temp <- do
        !temp <- VUM.new numEdges
        VU.forM_ (VU.generate numEdges id) $ \ !i -> do
          (!a,!b,!d) <- getEdge
          VUM.unsafeWrite temp i (a,b,d)
          VUM.modify fDegCount (+1) (a+1)
          VUM.modify bDegCount (+1) (b+1)
        VU.unsafeFreeze temp
      VU.foldM'
        (\ !total !i -> do
            !di <- VUM.unsafeRead fDegCount i
            VUM.unsafeWrite fDegCount i total
            return $! total+di)
        0 (VU.generate (numVert+1) id)
      VU.foldM'
        (\ !total !i -> do
            !di <- VUM.unsafeRead bDegCount i
            VUM.unsafeWrite bDegCount i total
            return $! total+di)
        0 (VU.generate (numVert+1) id)
      VU.forM_ temp $ \(!a,!b,!d) -> do
        !ia <- VUM.read fDegCount (a+1)
        VUM.unsafeWrite fDegCount (a+1) $! ia+1
        VUM.unsafeWrite fConnTable ia $! (b,d)
        !ib <- VUM.read bDegCount (b+1)
        VUM.unsafeWrite bDegCount (b+1) $! ib+1
        VUM.unsafeWrite bConnTable ib $! (a,d)


#define DefDebugAux(fct,ty,debugvar,dbg,rel)\
  fct :: ty; {-# INLINE fct #-}; fct | debugvar = dbg | otherwise = rel
#define DefDebug(fct,ty,rel) DefDebugAux(fct,ty,debug,Debug.Trace.fct,rel)
#define DefDebugC(fct,ty,rel) DefDebug(fct,ty,const (rel))

DefDebugC(trace, String -> a -> a, id)
DefDebug(traceId, String -> String, id)
DefDebugC(traceShow, Show b => b -> a -> a, id)
DefDebug(traceShowId, Show a => a -> a, id)
DefDebugC(traceStack, String -> a -> a, id)
DefDebugC(traceIO, String -> IO (), return ())
DefDebugC(traceM, Applicative f => String -> f (), pure ())
DefDebugC(traceShowM, (Show a, Applicative f) => a -> f (), pure ())
DefDebugC(traceEvent, String -> a -> a, id)
DefDebugC(traceEventIO, String -> IO (), pure ())
DefDebugC(traceMarker, String -> a -> a, id)
DefDebugC(traceMarkerIO, String -> IO (), pure ())

#undef DefDebugAux
#undef DefDebug
#undef DefDebugC

traceShowIO :: Show a => a -> IO ()
{-# INLINE traceShowIO #-}
traceShowIO | debug     = Debug.Trace.traceIO . show
            | otherwise = const $ pure ()

#define IL(f) {-# INLINE f #-}; f

putBuilder :: BSB.Builder -> IO ()
IL(putBuilder) = BSB.hPutBuilder stdout

printVecInLines, printVecInSpcSepLn ::
  (VG.Vector v a, ShowAsBuilder a) => v a -> IO ()
IL(printVecInLines) = putBuilder . v2BLines
IL(printVecInSpcSepLn) = putBuilder . v2BSpcSepLn

class ShowAsBuilder a where
  showAsBuilder :: a -> BSB.Builder
  default showAsBuilder :: (Show a) => a -> BSB.Builder
  IL(showAsBuilder) = BSB.string8 . show

-- Inconsistent with show
instance {-# INCOHERENT #-} (ShowAsBuilder a, VG.Vector v a) => ShowAsBuilder (v a) where
  IL(showAsBuilder) = v2BSpcSep

#define INS(t,f) instance ShowAsBuilder t where { IL(showAsBuilder)=f }
INS(Int,BSB.intDec)
INS(Int8,BSB.int8Dec)
INS(Int16,BSB.int16Dec)
INS(Int32,BSB.int32Dec)
INS(Int64,BSB.int64Dec)
INS(Word,BSB.wordDec)
INS(Word8,BSB.word8Dec)
INS(Word16,BSB.word16Dec)
INS(Word32,BSB.word32Dec)
INS(Word64,BSB.word64Dec)
INS(Integer,BSB.integerDec)
INS(Float,BSB.floatDec)
INS(Double,BSB.doubleDec)
-- INS(String,BSB.string8) -- Inconsistent with Show
-- INS(BS.ByteString,BSB.byteString) -- Inconsistent with Show
-- INS(BSL.ByteString,BSB.lazyByteString) -- Inconsisitent with Show
#undef INS


vConstrAccN
  :: forall v a b. VG.Vector v b
  => Int
  -> (v b -> a -> (a, b))
  -> a
  -> (a, v b)
{-# INLINE vConstrAccN #-}
vConstrAccN n f a0 = runST $ do
  vec <- VGM.new n
  go vec 0 a0
  where
    go :: forall s. VG.Mutable v s b -> Int -> a -> ST s (a, v b)
    go vec !i a | i >= n = (a,) <$> VG.unsafeFreeze vec
    go vec !i a = do
      vec <- VG.unsafeFreeze vec
      let (a', b) = f (VG.unsafeTake i vec) a
      vec <- VG.unsafeThaw vec
      VGM.unsafeWrite vec i b
      go vec (i+1) a'

-- Inconsistent with Show
instance (ShowAsBuilder a, ShowAsBuilder b) => ShowAsBuilder (a,b) where
  IL(showAsBuilder) = showTupAsBuilder
instance (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c) =>
  ShowAsBuilder (a,b,c) where
  IL(showAsBuilder) = showTup3AsBuilder
instance (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c, ShowAsBuilder d) =>
  ShowAsBuilder (a,b,c,d) where
  IL(showAsBuilder) = showTup4AsBuilder

IL(showTupAsBuilderWith)
  :: (a -> BSB.Builder) -> (b -> BSB.Builder) -> (a,b) -> BSB.Builder
showTupAsBuilderWith showA showB
  = \(a,b) -> (showA a <>) $ BSB.char7 ' ' <> showB b
IL(showTupAsBuilder) :: (ShowAsBuilder a, ShowAsBuilder b)
  => (a,b) -> BSB.Builder
showTupAsBuilder = showTupAsBuilderWith showAsBuilder showAsBuilder 

IL(showTup3AsBuilderWith) :: (a -> BSB.Builder) -> (b -> BSB.Builder) ->
  (c -> BSB.Builder) -> (a,b,c) -> BSB.Builder
showTup3AsBuilderWith showA showB showC
  = \(a,b,c) -> (showA a <>) $ (BSB.char7 ' ' <>) $ (showB b <>)
                $ (BSB.char7 ' ' <>) $ showC c
IL(showTup3AsBuilder) :: (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c)
  => (a,b,c) -> BSB.Builder
showTup3AsBuilder
  = showTup3AsBuilderWith showAsBuilder showAsBuilder showAsBuilder

IL(showTup4AsBuilderWith) :: (a -> BSB.Builder) -> (b -> BSB.Builder) ->
  (c -> BSB.Builder) -> (d -> BSB.Builder) -> (a,b,c,d) -> BSB.Builder
showTup4AsBuilderWith showA showB showC showD
  = \(a,b,c,d) -> (showA a <>) $ (BSB.char7 ' ' <>)
                  $ showTup3AsBuilderWith showB showC showD (b,c,d)
IL(showTup4AsBuilder) ::
  (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c, ShowAsBuilder d) =>
  (a,b,c,d) -> BSB.Builder
showTup4AsBuilder = showTup4AsBuilderWith showAsBuilder showAsBuilder
                    showAsBuilder showAsBuilder

v2BSpcSepLn, v2BSpcSep, v2BConcat, v2BLines ::
  (VG.Vector v a, ShowAsBuilder a)
  => v a -> BSB.Builder
IL(v2BSpcSepLn) = v2BSpcSepLnWith showAsBuilder
IL(v2BSpcSep) = v2BSpcSepWith showAsBuilder
IL(v2BConcat) = v2BConcatWith showAsBuilder
IL(v2BLines) = v2BLinesWith showAsBuilder


v2BSpcSepLnWith, v2BSpcSepWith, v2BConcatWith, v2BLinesWith ::
  (VG.Vector v a)
  => (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
IL(v2BSpcSepLnWith) = v2BSpcSepPostfWith $ BS.singleton '\n'
IL(v2BSpcSepWith) = v2BSpcSepPostfWith BS.empty
IL(v2BConcatWith) showFct = VG.foldr ((<>) . showFct) mempty
IL(v2BLinesWith) showFct
  = VG.foldr (\ a -> (showFct a <>) . (BSB.char7 '\n' <>)) mempty


v2BSpcSepPostf :: (VG.Vector v a, ShowAsBuilder a)
  => BS.ByteString -- ^ postfix
  -> v a -> BSB.Builder
IL(v2BSpcSepPostf) = (`v2BSpcSepPostfWith` showAsBuilder)

v2BSpcSepPostfWith :: (VG.Vector v a)
  => BS.ByteString -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
IL(v2BSpcSepPostfWith) = vecToBuilder BS.empty $ BS.singleton ' '

IL(vecToBuilder) :: (VG.Vector v a)
  => BS.ByteString -- ^ prefix
  -> BS.ByteString -- ^ separator
  -> BS.ByteString -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
vecToBuilder !prefix !separator !postfix
  = vecToBuilder_ (BSB.byteString prefix)
                  (BSB.byteString separator)
                  (BSB.byteString postfix)


IL(vecToBuilder_) :: (VG.Vector v a)
  => BSB.Builder -- ^ prefix
  -> BSB.Builder -- ^ separator
  -> BSB.Builder -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
vecToBuilder_ !prefix !separator !postfix showFct = \vec -> prefix <>
  VG.foldr
  (\ a rest !prefx -> prefx <> (showFct a <> rest separator))
  (const postfix) vec mempty

IL(evalVals) :: [a] -> [a]
evalVals xs = build $ \c n -> foldr (c $!) n xs
IL(forceVals) :: (NFData a) => [a] -> [a]
forceVals xs = build $ \c n -> foldr (c $!!) n xs

IL(readLnWith) :: StateT BS.ByteString Maybe a -> IO a
readLnWith parser = fromJust . evalStateT parser <$> BS.getLine
IL(readContentWith) :: StateT BSL.ByteString Maybe a -> IO a
readContentWith parser = fromJust . evalStateT parser <$> BSL.getContents

IL(getVecGLn) :: (VG.Vector v a) =>
  Int -> StateT BS.ByteString Maybe a -> IO (v a)
getVecGLn n s = VG.unfoldrN n (runStateT s) <$> BS.getLine
IL(getVecGRest) :: (VG.Vector v a) =>
  Int -> StateT BSL.ByteString Maybe a -> IO (v a)
getVecGRest n s = VG.unfoldrN n (runStateT s) <$> BSL.getContents
IL(getVecLn) :: Int -> StateT BS.ByteString Maybe a -> IO (V.Vector a)
getVecLn = getVecGLn
IL(getVecRest) :: Int -> StateT BSL.ByteString Maybe a -> IO (V.Vector a)
getVecRest = getVecGRest
IL(getVecULn) :: (VU.Unbox a) =>
  Int -> StateT BS.ByteString Maybe a -> IO (VU.Vector a)
getVecULn = getVecGLn
IL(getVecURest) :: (VU.Unbox a) =>
  Int -> StateT BSL.ByteString Maybe a -> IO (VU.Vector a)
getVecURest = getVecGRest

IL(ord8) :: Char -> Word8
ord8 = fromIntegral . fromEnum
IL(chr8) :: Word8 -> Char
chr8 = toEnum . fromIntegral

{-# INLINE rVecGExactN #-}
rVecGExactN :: (VG.Vector v a)
  => Int
  -> StateT s Maybe a
  -> StateT s Maybe (v a)
rVecGExactN n act = StateT $ \s0 -> runST $ do
  res <- VGM.new n
  let go !i s | i >= n = Just . (,s) <$> VG.unsafeFreeze res
      go !i s1 = case runStateT act s1 of
        Nothing -> return Nothing
        Just (a, s2) -> do
          VGM.unsafeWrite res i a
          go (i+1) s2
  go 0 s0

{-# INLINE rVecUExactN #-}
rVecUExactN :: (VU.Unbox a)
  => Int
  -> StateT s Maybe a
  -> StateT s Maybe (VU.Vector a)
rVecUExactN = rVecGExactN


class AtCoderIParsed bytestring where
  rInt :: StateT bytestring Maybe Int
  rInteger :: StateT bytestring Maybe Integer
  rStr :: MonadState bytestring m => m BS.ByteString
  rChar :: StateT bytestring Maybe Char
  rCharW :: StateT bytestring Maybe Word8
  dropSpecial :: MonadState bytestring m => m ()

skipSpecial
  :: (AtCoderIParsed bytestring, MonadState bytestring m)
  => m a -> m a
{-# INLINE skipSpecial #-}
skipSpecial = (dropSpecial *>)

rInt1 :: AtCoderIParsed bytestring => StateT bytestring Maybe Int
{-# INLINE rInt1 #-}
rInt1 = subtract 1 <$> rInt

rInteger1 :: AtCoderIParsed bytestring => StateT bytestring Maybe Integer
{-# INLINE rInteger1 #-}
rInteger1 = subtract 1 <$> rInteger

instance AtCoderIParsed BS.ByteString where
  {-# INLINE rInt #-}
  rInt = skipSpecial $ StateT BS.readInt
  {-# INLINE rInteger #-}
  rInteger = skipSpecial $ StateT BS.readInteger
  {-# INLINE rStr #-}
  rStr = skipSpecial $ state $ BSW.span (>= ord8 '!')
  {-# INLINE rChar #-}
  rChar = StateT BS.uncons
  {-# INLINE rCharW #-}
  rCharW = StateT BSW.uncons
  {-# INLINE dropSpecial #-}
  dropSpecial = modify $ BSW.dropWhile (< ord8 '!')


instance AtCoderIParsed BSL.ByteString where
  {-# INLINE rInt #-}
  rInt = skipSpecial $ StateT BSL.readInt
  {-# INLINE rInteger #-}
  rInteger = skipSpecial $ StateT BSL.readInteger
  {-# INLINE rStr #-}
  rStr = skipSpecial $ BSL.toStrict <$> state (BSLW.span (>= ord8 '!'))
  {-# INLINE rChar #-}
  rChar = StateT BSL.uncons
  {-# INLINE rCharW #-}
  rCharW = StateT BSLW.uncons
  {-# INLINE dropSpecial #-}
  dropSpecial = modify $ BSLW.dropWhile (< ord8 '!')

IL(linToMat) :: (VG.Vector v a) => Int -> Int -> v a -> V.Vector (v a)
linToMat h w lvec = vEvalElemsId $ V.generate h (\i -> VG.slice (i*w) w lvec)

IL(mLinToMat) :: (VGM.MVector v a) => Int -> Int -> v s a -> V.Vector (v s a)
mLinToMat h w lvec = vEvalElemsId $ V.generate h (\i -> VGM.slice (i*w) w lvec)
  
IL(unsafeAddrToSVec) :: Int -> Addr# -> VS.Vector Word8
unsafeAddrToSVec n addr
  = (`VS.unsafeFromForeignPtr0` n)
    $ unsafeDupablePerformIO
    $ newForeignPtr_ $ Ptr addr

IL(vEvalElemsId) :: (VG.Vector v a) => v a -> v a
vEvalElemsId = vMapFoldl (\ !_ !x -> (x,())) ()

IL(vEvalElems) :: (VG.Vector v a) => v a -> ()
vEvalElems = VG.foldl' (\ !_ !_ -> ()) () 

IL(vMapFoldl) :: (VG.Vector v b, VG.Vector v c) =>
  (a -> b -> (c,a)) -> a -> v b -> v c
vMapFoldl f a
  = VG.unstream . VFB.inplace (streamMapFoldl f a) id . VG.stream

streamMapFoldl :: (Functor m) =>
  (a -> b -> (c,a)) -> a -> VFSM.Stream m b -> VFSM.Stream m c
{-# INLINE_FUSED streamMapFoldl #-}
streamMapFoldl f a (VFSM.Stream step s) = VFSM.Stream step1 (a,s)
  where
    {-# INLINE_INNER step1 #-}
    step1 (a0,s0) =  (<$> step s0) $ \r -> case r of
      VFSM.Yield b s1 -> case f a0 b of (c,a1) -> VFSM.Yield c (a1,s1)
      VFSM.Skip    s1 -> VFSM.Skip (a0,s1)
      VFSM.Done       -> VFSM.Done

IL(svecToBS) :: VS.Vector Word8 -> BS.ByteString
svecToBS vec = BSU.fromForeignPtr ptr 0 len
  where (ptr, len) = VS.unsafeToForeignPtr0 vec

IL(vLength) :: VG.Vector v a => v a -> Int
vLength = VFB.length . VG.stream

unlessM, whenM :: (Monad m) => m Bool -> m () -> m ()
IL(whenM) = (. flip when) . (>>=)
IL(unlessM) = (. flip unless) . (>>=)


wrA :: (MArray a e m, A.Ix i) => a i e -> i -> e -> m ()
IL(wrA) = A.writeArray
rdA :: (MArray a e m, A.Ix i) => a i e -> i -> m e
IL(rdA) = A.readArray
mdA :: (MArray a b m, A.Ix i) => a i b -> (b -> b) -> i -> m (b, b)
IL(mdA) = \arr f !i -> do
  ai <- rdA arr i
  let fai = f ai 
  wrA arr i fai
  return (ai,fai)
mdA' :: (MArray a b m, A.Ix i) => a i b -> (b -> b) -> i -> m (b, b)
{-# INLINE mdA' #-}
mdA' = \arr f !i -> do
  !ai <- rdA arr i
  let !fai = f ai
  wrA arr i fai
  return (ai,fai)
swapA :: (MArray a e m, A.Ix i) => a i e -> i -> i -> m ()
IL(swapA) = \arr !i !j -> do
  ai <- rdA arr i
  wrA arr i =<< rdA arr j
  wrA arr j ai

#define D(f,r,d)\
  IL(f) :: Integral a=>a->d; f=fromIntegral;\
  IL(r) :: String->d; r=read
#define C(f,r,g,h,d) D(f,r,d);\
  g,h :: RealFrac a=>a->d; IL(g)=floor; IL(h)=ceiling
C(_toInteger_,readInteger,floorInteger,ceilInteger,Integer)
C(toInt,readInt,floorInt,ceilInt,Int)
C(toI8,readI8,floorI8,ceilI8,Int8)
C(toI16,readI16,floorI16,ceilI16,Int16)
C(toI32,readI32,floorI32,ceilI32,Int32)
C(toI64,readI64,floorI64,ceilI64,Int64)
C(toWord,readWord,floorWord,ceilWord,Word)
C(toW8,readW8,floorW8,ceilW8,Word8)
C(toW16,readW16,floorW16,ceilW16,Word16)
C(toW32,readW32,floorW32,ceilW32,Word32)
C(toW64,readW64,floorW64,ceilW64,Word64)
D(toDouble,readDouble,Double)
D(toFloat,readFloat,Float)
#undef D
#undef C

#define TS(f,a,m,s,init)\
  IL(f) :: forall e i s. (C(a,m) A.Ix i) => (i,i) -> init m (a i e); f
#define N(f,g,h,s,a,m)\
  TS(f,a,m,s,e->)=A.newArray;\
  TS(g,a,m,s,)=A.newArray_;\
  TS(h,a,m,s,[e]->)=A.newListArray
#define C(a,m)
N(newIOA,newIOA_,newIOAL,,IOArray,IO)
N(newSTA,newSTA_,newSTAL,s,(STArray s),(ST s))
#undef C
#define C(a,m) MArray a e m, 
N(newIOUA,newIOUA_,newIOUAL,,IOUArray,IO)
N(newSTUA,newSTUA_,newSTUAL,s,(STUArray s),(ST s))
#undef C
#undef N
#undef TS

#undef IL

Submission Info

Submission Time
Task G - Last Major City
User gksato
Language Haskell (GHC 9.4.5)
Score 600
Code Size 37566 Byte
Status AC
Exec Time 551 ms
Memory 29308 KB

Compile Error

app/Main.hs:217:4: warning: [-Wname-shadowing]
    This binding for ‘len’ shadows the existing binding
      bound at app/Main.hs:216:29
    |
217 |   !len <- DMut.readRef len
    |    ^^^

app/Main.hs:242:39: warning: [-Wunused-matches]
    Defined but not used: ‘arr’
    |
242 | fcbhFullHeapify hp@(FixedCapBHeap len arr) = stToPrim $ do
    |                                       ^^^

app/Main.hs:243:4: warning: [-Wname-shadowing]
    This binding for ‘len’ shadows the existing binding
      bound at app/Main.hs:242:35
    |
243 |   !len <- DMut.readRef len
    |    ^^^

app/Main.hs:342:10: warning: [-Wname-shadowing]
    This binding for ‘rest’ shadows the existing binding
      bound at app/Main.hs:339:38
    |
342 |         !rest <- (\f -> VU.foldM' f rest $ edgesOut grph v) $ \ !heap (!w,!dvw) -> do
    |          ^^^^

app/Main.hs:353:4: warning: [-Wname-shadowing]
    This binding for ‘src1’ shadows the existing binding
      bound at app/Main.hs:352:16
    |
353 |   !src1 <- M.outerSliceMArrayM mat src1
    |    ^^^^

app/Main.hs:354:4: warning: [-Wname-shadowing]
    This binding for ‘src2’ shadows the existing binding
      bound at app/Main.hs:352:22
    |
354 |   !src2 <- M.outerSliceMArrayM mat src2
    |    ^^^^

app/Main.hs:355:4: warning: [-Wname-shadowing]
    This binding for ‘dst’ shadows the existing binding
      bound at app/Main.hs:352:28
    |
355 |   !dst <- M.outerSliceMArrayM mat dst
    |    ^^^

app/Main.hs:426:4: warning: [-Wname-shadowing]
    This binding for ‘gd’ shadows the existing binding
      bound at app/Main.hs:424:4
    |
426 |   !gd <- unsafeFreezeGraphData gd
    |    ^^

app/Main.hs:518:7: warning: [-Wunused-do-bind]
    A do-notation statement discarded a result of type ‘Int’
    Suggested fix:
      Suppress this warning by saying
        ‘_ <- VU.foldM'
                (\ !total !i
                   -> do !di <- VUM.unsafeRead degCount i
                         VUM.unsafeWrite degCount i total
                         return $! total + di)
                0 (VU.generate (numVert + 1) id)’
    |
518 |       VU.foldM'
    |       ^^^^^^^^^...

app/Main.hs:549:7: warning: [-Wunused-do-bind]
    A do-notation statement discarded a result of type ‘Int’
    Suggested fix:
      Suppress this warning by saying
        ‘_ <- VU.foldM'
                (\ !total !i
                   -> do !di <- VUM.unsafeRead degCount i
                         VUM.unsafeWrite degCount i total
                         return $! total + di)
                0 (VU.generate (numVert + 1) id)’
    |
549 |       VU.foldM'
    |       ^^^^^^^^^...

app/Main.hs:582:7: warning: [-Wunused-do-bind]
    A do-notation statement discarded a result of type ‘Int’
    Suggested fix:
      Suppress this warning by saying
        ‘_ <- VU.foldM'
                (\ !total !i
                   -> do !di <- VUM.unsafeRead fDegCount i
                         VUM.unsafeWrite fDegCount i total
                         return $! total + di)
                0 (VU.generate (numVert + 1) id)’
    |
582 |       VU.foldM'
    |       ^^^^^^^^^...

app/Main.hs:588:7: warning: [-Wunused-do-bind]
    A do-notation statement discarded a result of type ‘Int’
    Suggested fix:
      Suppress this warning by saying
        ‘_ <- VU.foldM'
                (\ !total !i
                   -> do !di <- VUM.unsafeRead bDegCount i
                         VUM.unsafeWrite bDegCount i total
                         return $! total + di)
                0 (VU.generate (numVert + 1) id)’
    |
588 |       VU.foldM'
    |       ^^^^^^^^^...

app/Main.hs:683:7: warning: [-Wname-shadowing]
    This binding for ‘vec’ shadows the existing binding
      bound at app/Main.hs:682:8
    |
683 |       vec <- VG.unsafeFreeze vec
    |       ^^^

app/Main.hs:685:7: warning: [-Wname-shadowing]
    This binding for ‘vec’ shadows the existing binding
      bound at app/Main.hs:683:7
    |
685 |       vec <- VG.unsafeThaw vec
    |       ^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 40
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt, 04_killer_00.txt, 04_killer_01.txt, 04_killer_02.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 7980 KB
00_sample_01.txt AC 1 ms 7892 KB
00_sample_02.txt AC 1 ms 8044 KB
01_random_00.txt AC 40 ms 14048 KB
01_random_01.txt AC 10 ms 13484 KB
01_random_02.txt AC 60 ms 15072 KB
01_random_03.txt AC 401 ms 27856 KB
01_random_04.txt AC 181 ms 19668 KB
01_random_05.txt AC 61 ms 14484 KB
01_random_06.txt AC 4 ms 12832 KB
01_random_07.txt AC 21 ms 13724 KB
01_random_08.txt AC 6 ms 13148 KB
01_random_09.txt AC 251 ms 19764 KB
01_random_10.txt AC 381 ms 26192 KB
01_random_11.txt AC 311 ms 23172 KB
01_random_12.txt AC 111 ms 16716 KB
01_random_13.txt AC 8 ms 13148 KB
01_random_14.txt AC 7 ms 13056 KB
01_random_15.txt AC 521 ms 29064 KB
01_random_16.txt AC 431 ms 28840 KB
01_random_17.txt AC 481 ms 28988 KB
01_random_18.txt AC 4 ms 12980 KB
01_random_19.txt AC 30 ms 14248 KB
01_random_20.txt AC 60 ms 15216 KB
01_random_21.txt AC 551 ms 29256 KB
01_random_22.txt AC 471 ms 29308 KB
01_random_23.txt AC 491 ms 29168 KB
02_random2_00.txt AC 431 ms 29112 KB
02_random2_01.txt AC 421 ms 29200 KB
02_random2_02.txt AC 431 ms 29196 KB
02_random2_03.txt AC 431 ms 29108 KB
02_random2_04.txt AC 431 ms 29192 KB
03_handmade_00.txt AC 1 ms 7988 KB
03_handmade_01.txt AC 3 ms 12260 KB
03_handmade_02.txt AC 3 ms 12276 KB
03_handmade_03.txt AC 262 ms 28892 KB
03_handmade_04.txt AC 161 ms 28952 KB
04_killer_00.txt AC 521 ms 29192 KB
04_killer_01.txt AC 491 ms 29192 KB
04_killer_02.txt AC 521 ms 29256 KB


2025-04-01 (Tue)
13:38:24 +00:00