結果

問題 No.629 グラフの中に眠る門松列
ユーザー ducktail
提出日時 2018-07-12 14:06:15
言語 Haskell
(9.10.1)
結果
AC  
実行時間 115 ms / 4,000 ms
コード長 1,240 bytes
コンパイル時間 11,539 ms
コンパイル使用メモリ 204,292 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2024-10-01 21:03:42
合計ジャッジ時間 13,771 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 36
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Applicative ((<$>), (<*>))
import Control.Monad (replicateM, guard)
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.List (unfoldr, foldl', find)
import Data.Char (isSpace)
import Data.Vector (Vector, (!), (//))
import qualified Data.Vector as V

main :: IO ()
main = do
  [n, m] <- readil B.readInt <$> B.getLine
  solve n <$> (readil B.readInt <$> B.getLine) <*> replicateM m (readil B.readInt <$> B.getLine) >>= putStrLn

solve :: Int -> [Int] -> [[Int]] -> String
solve n al es = maybe "NO" (const "YES") $ find kdmt ls
  where g = mkg n es
        av = V.fromList al
        ls = do
          i <- [1..n]
          j <- g ! i
          k <- g ! j
          guard $ i /= k
          return (av ! (i-1), av ! (j-1), av ! (k-1))
          
mkg :: Int -> [[Int]] -> Vector [Int]
mkg n es = foldl' f (V.replicate (n+1) []) es
  where f v [x, y] = v // [(x, y : v ! x),(y, x : v ! y)]

kdmt :: (Int, Int, Int) -> Bool
kdmt (x, y, z) = x /= z && ((y > x && y > z) || (y < x && y < z))

readil :: Integral a =>  (ByteString -> Maybe (a, ByteString)) -> ByteString -> [a]
readil f = unfoldr g
  where g s = do
          (n, s') <- f s
          return (n, B.dropWhile isSpace s')
0