結果

問題 No.1605 Matrix Shape
ユーザー 炭酸水炭酸水
提出日時 2022-05-19 23:14:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,760 ms / 2,000 ms
コード長 1,244 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 11,924 KB
実行使用メモリ 61,608 KB
最終ジャッジ日時 2023-10-19 07:40:36
合計ジャッジ時間 23,062 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,252 KB
testcase_01 AC 31 ms
10,252 KB
testcase_02 AC 31 ms
10,252 KB
testcase_03 AC 34 ms
10,252 KB
testcase_04 AC 33 ms
10,252 KB
testcase_05 AC 30 ms
10,252 KB
testcase_06 AC 30 ms
10,252 KB
testcase_07 AC 29 ms
10,252 KB
testcase_08 AC 31 ms
10,252 KB
testcase_09 AC 30 ms
10,252 KB
testcase_10 AC 31 ms
10,252 KB
testcase_11 AC 30 ms
10,252 KB
testcase_12 AC 30 ms
10,252 KB
testcase_13 AC 30 ms
10,252 KB
testcase_14 AC 30 ms
10,252 KB
testcase_15 AC 31 ms
10,252 KB
testcase_16 AC 31 ms
10,252 KB
testcase_17 AC 30 ms
10,252 KB
testcase_18 AC 30 ms
10,252 KB
testcase_19 AC 1,760 ms
61,348 KB
testcase_20 AC 1,324 ms
35,792 KB
testcase_21 AC 1,306 ms
35,760 KB
testcase_22 AC 1,702 ms
59,760 KB
testcase_23 AC 1,726 ms
61,608 KB
testcase_24 AC 1,715 ms
61,072 KB
testcase_25 AC 720 ms
10,332 KB
testcase_26 AC 728 ms
10,348 KB
testcase_27 AC 720 ms
10,348 KB
testcase_28 AC 721 ms
10,348 KB
testcase_29 AC 727 ms
10,332 KB
testcase_30 AC 1,202 ms
33,972 KB
testcase_31 AC 1,176 ms
33,960 KB
testcase_32 AC 971 ms
21,864 KB
testcase_33 AC 970 ms
21,888 KB
testcase_34 AC 719 ms
10,268 KB
testcase_35 AC 1,254 ms
47,760 KB
testcase_36 AC 761 ms
35,788 KB
権限があれば一括ダウンロードができます

ソースコード

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