結果

問題 No.1605 Matrix Shape
ユーザー rin204
提出日時 2021-07-16 22:15:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,363 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 102,784 KB
最終ジャッジ日時 2024-07-06 09:34:41
合計ジャッジ時間 5,903 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
max_ = 2 * 10 ** 5
edges = [[] for _ in range(max_)]
in_cnt = [0] * max_
out_cnt = [0] * max_
for _ in range(n):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    edges[h].append(w)
    in_cnt[h] += 1
    out_cnt[w] += 1
    
s = -1
g = -1

for j, (i, o) in enumerate(zip(in_cnt, out_cnt)):
    if i == o:
        continue
    if i == o + 1:
        if s == -1:
            s = j
        else:
            print(0)
            exit()
    elif i + 1 == o:
        if g == -1:
            g = j
        else:
            print(0)
            exit()
    else:
        print(0)
        exit()

if s == g == -1:
    stack = [h]
    used = [False] * max_
    used[h] = True
    while stack:
        pos = stack.pop()
        for npos in edges[pos]:
            if used[npos]:
                continue
            used[npos] = True
            
    for i in range(max_):
        if not used[i] and in_cnt[i] != 0:
            print(0)
            exit()
    

    ans = 0
    for i in in_cnt:
        if i > 0: ans += 1
    print(ans)
    exit()

stack = [s]
used = [False] * max_
used[s] = True
while stack:
    pos = stack.pop()
    for npos in edges[pos]:
        if used[npos]:
            continue
        used[npos] = True
        
for i in range(max_):
    if not used[i] and in_cnt[i] != 0:
        print(0)
        exit()

print(1)
0