結果

問題 No.1605 Matrix Shape
ユーザー shotoyooshotoyoo
提出日時 2021-07-16 22:47:13
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,823 bytes
コンパイル時間 336 ms
コンパイル使用メモリ 86,536 KB
実行使用メモリ 181,508 KB
最終ジャッジ日時 2023-09-20 15:10:17
合計ジャッジ時間 9,737 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,172 KB
testcase_01 AC 75 ms
71,088 KB
testcase_02 AC 75 ms
71,008 KB
testcase_03 AC 74 ms
71,124 KB
testcase_04 AC 75 ms
71,192 KB
testcase_05 AC 74 ms
71,180 KB
testcase_06 AC 73 ms
71,032 KB
testcase_07 AC 75 ms
71,104 KB
testcase_08 AC 73 ms
71,196 KB
testcase_09 AC 74 ms
70,800 KB
testcase_10 AC 74 ms
70,944 KB
testcase_11 AC 74 ms
71,088 KB
testcase_12 AC 72 ms
70,804 KB
testcase_13 AC 88 ms
80,196 KB
testcase_14 AC 75 ms
71,052 KB
testcase_15 AC 75 ms
70,948 KB
testcase_16 AC 74 ms
71,044 KB
testcase_17 AC 74 ms
71,052 KB
testcase_18 AC 75 ms
71,052 KB
testcase_19 AC 565 ms
181,508 KB
testcase_20 AC 413 ms
149,392 KB
testcase_21 AC 403 ms
133,520 KB
testcase_22 AC 404 ms
144,516 KB
testcase_23 AC 523 ms
178,092 KB
testcase_24 WA -
testcase_25 AC 251 ms
137,300 KB
testcase_26 AC 256 ms
140,156 KB
testcase_27 AC 243 ms
135,740 KB
testcase_28 AC 244 ms
135,636 KB
testcase_29 AC 244 ms
135,728 KB
testcase_30 AC 400 ms
143,504 KB
testcase_31 AC 379 ms
127,200 KB
testcase_32 AC 343 ms
120,872 KB
testcase_33 AC 251 ms
97,144 KB
testcase_34 AC 257 ms
150,336 KB
testcase_35 AC 281 ms
120,128 KB
testcase_36 AC 165 ms
90,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))


n = int(input())
hw = []
m = 0
for _ in range(n):
    h,w = list(map(int, input().split()))
    hw.append((h,w))
    m = max(m, h, w)
ns = [[] for _ in range(m)]
rns = [[] for _ in range(m)]
u = 0
for h,w in hw:
    ns[h-1].append(w-1)
    rns[w-1].append(h-1)
#     ns[u].append(n+w-1)
#     ns[n+h-1].append(u)
#     rns[n+w-1].append(u)
#     rns[u].append(n+h-1)
    u += 1
v0 = v1 = 0
start = None
for u in range(m):
    if len(ns[u])==len(rns[u]):
        pass
    elif len(ns[u])+1 == len(rns[u]):
        v0 += 1
    elif len(rns[u])+1 == len(ns[u]):
        v1 += 1
        start = u
    else:
        v0 = v1 = 100
        break
def euler_d(ns, start=0):
    """有向グラフのオイラー閉路
    パスの場合はstartを奇数次数頂点にする
    """
    n = len(ns)
    p = []
    q = [start]
    r = []
    vs = [0]*n
    while q:
        u = q.pop()
        u0 = u
        while vs[u]<len(ns[u]):
            v = ns[u][vs[u]]
            vs[u] += 1
            u = v
            r.append(v)
        while r:
            q.append(r.pop())
        p.append(u0)
    return p
if v0==v1==0:
    # ひとつかどうか判定
    for start in range(len(ns)):
        if ns[start]:
            break
    res = euler_d(ns, start)
    if len(res)==n+1:
        s = set()
        l = res[:-1]
        for i in range(len(l)):
            h0 = l[i]
            w1 = l[i-1]
            s.add((h0))
        ans = len(s)
    else:
        ans = 0
elif v0==v1==1:
    res = euler_d(ns, start)
    if len(res)==n+1:
        ans = 1
    else:
        ans = 0
else:
    ans = 0
print(ans)
0