結果

問題 No.1605 Matrix Shape
ユーザー 炭酸水
提出日時 2022-05-19 23:14:09
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,551 ms / 2,000 ms
コード長 1,244 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 62,116 KB
最終ジャッジ日時 2024-09-19 03:59:13
合計ジャッジ時間 21,045 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
P_C = {}
C_P = {}

v = set()
z = {}
for _ in range(n):
    x, y = map(int, input().split())
    if x in v:
        z[x] += 1
    else:
        v.add(x)
        z[x] = 1
    if y in v:
        z[y] -= 1
    else:
        v.add(y)
        z[y] = -1
    
    x_P = C_P.get(x, 0)
    y_P = C_P.get(y, 0)
    
    if x_P == 0 and y_P == 0:
        C_P[x] = x
        C_P[y] = x
        P_C[x] = [x, y]
    
    elif x_P == 0:
        C_P[x] = y_P
        P_C[y_P].append(x)
    
    elif y_P == 0:
        C_P[y] = x_P
        P_C[x_P].append(y)
    
    elif x_P != y_P:
        if len(P_C[x_P]) >= len(P_C[y_P]):
            P = P_C[y_P].copy()
            P_0 = x_P
            P_1 = y_P
        else:
            P = P_C[x_P].copy()
            P_0 = y_P
            P_1 = x_P        
        
        for i in P:
            C_P[i] = P_0
        P_C[P_0] += P
        del P_C[P_1]

count_p1 = 0
count_0 = 0
count_m1 = 0

for i in v:
    if z[i] == 0:
        count_0 += 1
    elif z[i] == 1:
        count_p1 += 1
    elif z[i] == -1:
        count_m1 += 1

if len(P_C) ==1 and count_m1 == 1 and count_p1 == 1 and count_0 == len(v) - 2:
    print(1)
elif len(P_C) ==1 and count_0 == len(v):
    print(len(v))
else:
    print(0)
0