結果

問題 No.1605 Matrix Shape
ユーザー convexineqconvexineq
提出日時 2021-07-16 21:52:50
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,512 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 87,272 KB
実行使用メモリ 86,508 KB
最終ジャッジ日時 2023-09-20 13:42:02
合計ジャッジ時間 9,299 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
82,376 KB
testcase_01 AC 87 ms
82,580 KB
testcase_02 AC 85 ms
82,232 KB
testcase_03 AC 89 ms
82,780 KB
testcase_04 AC 90 ms
82,808 KB
testcase_05 AC 90 ms
82,624 KB
testcase_06 AC 90 ms
82,684 KB
testcase_07 AC 85 ms
82,592 KB
testcase_08 AC 92 ms
82,620 KB
testcase_09 AC 85 ms
82,456 KB
testcase_10 AC 85 ms
82,516 KB
testcase_11 AC 84 ms
82,464 KB
testcase_12 AC 88 ms
82,576 KB
testcase_13 AC 86 ms
82,528 KB
testcase_14 AC 86 ms
82,396 KB
testcase_15 AC 85 ms
82,636 KB
testcase_16 AC 87 ms
82,404 KB
testcase_17 AC 87 ms
82,316 KB
testcase_18 AC 87 ms
82,468 KB
testcase_19 AC 350 ms
84,616 KB
testcase_20 AC 349 ms
84,400 KB
testcase_21 AC 339 ms
84,224 KB
testcase_22 AC 336 ms
84,416 KB
testcase_23 AC 335 ms
84,440 KB
testcase_24 AC 339 ms
84,180 KB
testcase_25 AC 263 ms
84,136 KB
testcase_26 AC 264 ms
84,244 KB
testcase_27 AC 264 ms
83,956 KB
testcase_28 WA -
testcase_29 AC 261 ms
84,020 KB
testcase_30 AC 354 ms
84,456 KB
testcase_31 AC 341 ms
84,628 KB
testcase_32 AC 344 ms
84,596 KB
testcase_33 AC 331 ms
84,352 KB
testcase_34 AC 199 ms
86,508 KB
testcase_35 AC 286 ms
84,340 KB
testcase_36 AC 237 ms
84,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]

n = int(input())
N = 2*10**5+1
ind = [0]*N
out = [0]*N
UF = UnionFind(N)
for _ in range(n):
    h,w = map(int,input().split())
    ind[h] += 1
    out[w] += 1
    UF.merge(h,w)
    r = UF.root(h)
nonzero = [i for i in range(N) if ind[i] != out[i]]
if len(nonzero) == 0:
    ans = sum(ind[i]+out[i] > 0 for i in range(N))
elif len(nonzero) == 2:
    i,j = nonzero
    if abs(ind[i] - out[i]) == 1:
        ans = 1
    else:
        ans = 0
else:
    ans = 0
for i in range(N):
    if UF.size[i] != 1 and UF.root(i) != r:
        ans = 0
print(ans)
0