結果

問題 No.583 鉄道同好会
コンテスト
ユーザー pekempey
提出日時 2017-10-28 03:35:58
言語 Haskell
(9.14.1)
コンパイル:
ghc -rtsopts -with-rtsopts=-K1G -o a.out -O2 _filename_
実行:
./a.out
結果
AC  
実行時間 1,268 ms / 2,000 ms
コード長 1,194 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,360 ms
コンパイル使用メモリ 195,328 KB
実行使用メモリ 64,128 KB
最終ジャッジ日時 2026-05-16 04:29:57
合計ジャッジ時間 5,668 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.14.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
Main.hs:20:50: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Data.List, but defined in GHC.Internal.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching, 'Data.List.uncons' or 'Data.Maybe.listToMaybe' instead. Consider refactoring to use "Data.List.NonEmpty"."
   |
20 |                       then let reachable = dfs ((head . head) g) [] (toArray n g)
   |                                                  ^^^^

Main.hs:20:57: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Data.List, but defined in GHC.Internal.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching, 'Data.List.uncons' or 'Data.Maybe.listToMaybe' instead. Consider refactoring to use "Data.List.NonEmpty"."
   |
20 |                       then let reachable = dfs ((head . head) g) [] (toArray n g)
   |                                                         ^^^^

[2 of 2] Linking a.out

ソースコード

diff #
raw source code

import Data.List
import Control.Applicative
import Debug.Trace
import Data.Array
import Data.Array.ST
import Control.Monad
import Control.Monad.ST

main :: IO ()
main = solve <$> (map read . words <$> getLine) <*> (map (map read . words) . lines <$> getContents) >>= putStrLn . yesno

yesno :: Bool -> String
yesno True = "YES"
yesno False = "NO"

solve :: [Int] -> [[Int]] -> Bool
solve [n, m] [] = True
solve [n, m] g = let deg = (map length . group . sort . concat) g
                 in if (all even) deg || (length . filter odd) deg == 2
                      then let reachable = dfs ((head . head) g) [] (toArray n g)
                           in (sort . nub) reachable == (sort . nub . concat) g
                      else False

toArray :: Int -> [[Int]] -> Array Int [Int]
toArray n es = runST $ do
  g <- newArray (0, n-1) [] :: ST s (STArray s Int [Int])
  forM_ es $ \[x, y] -> do
    readArray g x >>= writeArray g x . (y:)
    readArray g y >>= writeArray g y . (x:)
  freeze g

dfs :: Int -> [Int] -> Array Int [Int] -> [Int]
dfs u vis g = foldl' f (u:vis) (g ! u)
  where
    f :: [Int] -> Int -> [Int]
    f vis v
      | v `elem` vis = vis
      | otherwise    = dfs v vis g
0