結果

問題 No.583 鉄道同好会
ユーザー 6soukiti296soukiti29
提出日時 2017-10-27 23:24:29
言語 Nim
(2.0.2)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,252 bytes
コンパイル時間 2,992 ms
コンパイル使用メモリ 68,108 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-12 15:22:18
合計ジャッジ時間 5,035 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 19 ms
4,376 KB
testcase_12 AC 25 ms
4,380 KB
testcase_13 AC 25 ms
4,380 KB
testcase_14 AC 26 ms
4,376 KB
testcase_15 AC 33 ms
4,376 KB
testcase_16 AC 60 ms
4,376 KB
testcase_17 AC 72 ms
4,376 KB
testcase_18 AC 72 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,strutils

type
    unionfindtree[I : static[int]] = array[I,int]

proc find(U : unionfindtree; a : int, b :int): bool=
    var
        s = a
        t = b
    while s != U[s]:
        s = U[s]
    while t != U[t]:
        t = U[t]
    return s == t
    
proc union(U : var  unionfindtree; a : int ; b : int)=
    if U.find(a,b):
        return
    var
        s = a
        t = b
        t2 : int
    while s != U[s]:
        s = U[s]
    while t != U[t]:
        t2 = U[t]
        U[t] = s
        t = t2
    U[t] = s
proc root(U : unionfindtree, a :int):int=
    var
        s = a
    while s != U[s]:
        s = U[s]
    return s

var
    N, M : int
    a, b : int
    ut : unionfindtree[501]
    cnt : array[501, int]
    m = -1
    cntodd : int

for i in 0..500:
    ut[i] = i

(N, M) = stdin.readline.split.map(parseInt)

for n in 1..M:
    (a, b) = stdin.readline.split.map(parseInt)
    ut.union(a, b)
    cnt[a] += 1
    cnt[b] += 1
    
for i in 0..<N:
    if (cnt[i] and 1) == 1:
        cntodd += 1
    var
        r = ut.root(i)
    if r == i:
        continue
    elif m == -1:
        m = r
    elif r != m:
        m = -2
        break
    

if m == -2:
    echo "NO"
elif cntodd > 2:
    echo "NO"
else:
    echo "YES"
0