結果

問題 No.1605 Matrix Shape
ユーザー shotoyooshotoyoo
提出日時 2021-07-17 00:32:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 510 ms / 2,000 ms
コード長 1,876 bytes
コンパイル時間 1,282 ms
コンパイル使用メモリ 86,888 KB
実行使用メモリ 180,796 KB
最終ジャッジ日時 2023-09-20 17:03:56
合計ジャッジ時間 11,120 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,192 KB
testcase_01 AC 70 ms
71,116 KB
testcase_02 AC 70 ms
71,296 KB
testcase_03 AC 70 ms
71,188 KB
testcase_04 AC 70 ms
71,064 KB
testcase_05 AC 71 ms
71,144 KB
testcase_06 AC 70 ms
71,316 KB
testcase_07 AC 69 ms
71,176 KB
testcase_08 AC 70 ms
71,336 KB
testcase_09 AC 71 ms
71,228 KB
testcase_10 AC 72 ms
71,264 KB
testcase_11 AC 70 ms
71,220 KB
testcase_12 AC 69 ms
71,280 KB
testcase_13 AC 85 ms
80,372 KB
testcase_14 AC 70 ms
70,948 KB
testcase_15 AC 70 ms
71,104 KB
testcase_16 AC 70 ms
71,204 KB
testcase_17 AC 72 ms
71,392 KB
testcase_18 AC 71 ms
71,024 KB
testcase_19 AC 510 ms
180,796 KB
testcase_20 AC 469 ms
149,544 KB
testcase_21 AC 370 ms
133,988 KB
testcase_22 AC 366 ms
144,692 KB
testcase_23 AC 494 ms
178,068 KB
testcase_24 AC 472 ms
179,956 KB
testcase_25 AC 240 ms
137,340 KB
testcase_26 AC 246 ms
140,188 KB
testcase_27 AC 231 ms
135,688 KB
testcase_28 AC 234 ms
136,052 KB
testcase_29 AC 233 ms
135,936 KB
testcase_30 AC 369 ms
143,192 KB
testcase_31 AC 358 ms
127,560 KB
testcase_32 AC 310 ms
121,004 KB
testcase_33 AC 229 ms
97,404 KB
testcase_34 AC 246 ms
150,432 KB
testcase_35 AC 262 ms
120,028 KB
testcase_36 AC 157 ms
90,756 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
end = None
for u in range(m):
    if len(ns[u])==len(rns[u]):
        pass
    elif len(ns[u])+1 == len(rns[u]):
        v0 += 1
        end = u
    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:
    ns[end].append(start)
    res = euler_d(ns, start)
    if len(res)==n+2:
        ans = 1
    else:
        ans = 0
else:
    ans = 0
print(ans)
0