結果

問題 No.1605 Matrix Shape
ユーザー trineutrontrineutron
提出日時 2021-07-16 21:57:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,175 ms / 2,000 ms
コード長 976 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 20,936 KB
最終ジャッジ日時 2023-09-20 13:50:20
合計ジャッジ時間 20,828 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
20,852 KB
testcase_01 AC 154 ms
20,880 KB
testcase_02 AC 45 ms
20,876 KB
testcase_03 AC 149 ms
20,832 KB
testcase_04 AC 44 ms
20,876 KB
testcase_05 AC 152 ms
20,872 KB
testcase_06 AC 147 ms
20,936 KB
testcase_07 AC 45 ms
20,836 KB
testcase_08 AC 151 ms
20,872 KB
testcase_09 AC 145 ms
20,800 KB
testcase_10 AC 154 ms
20,892 KB
testcase_11 AC 72 ms
20,824 KB
testcase_12 AC 153 ms
20,880 KB
testcase_13 AC 150 ms
20,864 KB
testcase_14 AC 156 ms
20,836 KB
testcase_15 AC 158 ms
20,852 KB
testcase_16 AC 151 ms
20,800 KB
testcase_17 AC 73 ms
20,932 KB
testcase_18 AC 78 ms
20,864 KB
testcase_19 AC 1,175 ms
20,928 KB
testcase_20 AC 1,032 ms
20,868 KB
testcase_21 AC 1,048 ms
20,844 KB
testcase_22 AC 1,005 ms
20,888 KB
testcase_23 AC 1,130 ms
20,936 KB
testcase_24 AC 1,160 ms
20,900 KB
testcase_25 AC 793 ms
20,888 KB
testcase_26 AC 830 ms
20,860 KB
testcase_27 AC 833 ms
20,812 KB
testcase_28 AC 706 ms
20,920 KB
testcase_29 AC 813 ms
20,848 KB
testcase_30 AC 990 ms
20,868 KB
testcase_31 AC 1,000 ms
20,916 KB
testcase_32 AC 900 ms
20,836 KB
testcase_33 AC 791 ms
20,840 KB
testcase_34 AC 754 ms
20,864 KB
testcase_35 AC 889 ms
20,860 KB
testcase_36 AC 529 ms
20,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())

par = [i for i in range(200000)]
sz = [1] * 200000

def find(k):
    if par[k] == k:
        return k
    par[k] = find(par[k])
    return par[k]

def merge(k, l):
    k = find(k)
    l = find(l)
    if k == l:
        return
    if sz[k] > sz[l]:
        k, l = l, k
    par[k] = l
    sz[l] += sz[k]

count_h = [0] * 200000
count_w = [0] * 200000
for i in range(n):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    count_h[h] += 1
    count_w[w] += 1
    merge(h, w)

root = -1
for i in range(200000):
    if count_h[i] + count_w[i]:
        if root == -1:
            root = find(i)
        elif find(i) != root:
            print(0)
            exit()

ans = 0
diff = []
for i in range(200000):
    if abs(count_h[i] - count_w[i]) > 1:
        print(0)
        exit()
    elif count_h[i] != count_w[i]:
        diff.append(i)
    elif count_h[i]:
        ans += 1
if len(diff) > 2:
    print(0)
elif diff:
    print(1)
else:
    print(ans)
0