結果
問題 | No.1605 Matrix Shape |
ユーザー | mkawa2 |
提出日時 | 2021-07-17 00:33:10 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 562 ms / 2,000 ms |
コード長 | 2,108 bytes |
コンパイル時間 | 246 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 33,384 KB |
最終ジャッジ日時 | 2024-07-06 12:05:48 |
合計ジャッジ時間 | 11,193 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
15,488 KB |
testcase_01 | AC | 50 ms
15,616 KB |
testcase_02 | AC | 36 ms
15,360 KB |
testcase_03 | AC | 50 ms
15,360 KB |
testcase_04 | AC | 32 ms
15,488 KB |
testcase_05 | AC | 50 ms
15,744 KB |
testcase_06 | AC | 50 ms
15,360 KB |
testcase_07 | AC | 32 ms
15,488 KB |
testcase_08 | AC | 50 ms
15,488 KB |
testcase_09 | AC | 50 ms
15,616 KB |
testcase_10 | AC | 50 ms
15,488 KB |
testcase_11 | AC | 32 ms
15,616 KB |
testcase_12 | AC | 50 ms
15,488 KB |
testcase_13 | AC | 50 ms
15,488 KB |
testcase_14 | AC | 50 ms
15,488 KB |
testcase_15 | AC | 32 ms
15,488 KB |
testcase_16 | AC | 32 ms
15,488 KB |
testcase_17 | AC | 33 ms
15,616 KB |
testcase_18 | AC | 31 ms
15,488 KB |
testcase_19 | AC | 562 ms
33,232 KB |
testcase_20 | AC | 547 ms
24,324 KB |
testcase_21 | AC | 527 ms
24,280 KB |
testcase_22 | AC | 542 ms
33,384 KB |
testcase_23 | AC | 560 ms
33,272 KB |
testcase_24 | AC | 554 ms
33,224 KB |
testcase_25 | AC | 472 ms
15,616 KB |
testcase_26 | AC | 473 ms
15,488 KB |
testcase_27 | AC | 469 ms
15,616 KB |
testcase_28 | AC | 472 ms
15,488 KB |
testcase_29 | AC | 480 ms
15,616 KB |
testcase_30 | AC | 533 ms
24,332 KB |
testcase_31 | AC | 527 ms
24,456 KB |
testcase_32 | AC | 495 ms
19,112 KB |
testcase_33 | AC | 474 ms
19,112 KB |
testcase_34 | AC | 416 ms
15,616 KB |
testcase_35 | AC | 429 ms
33,316 KB |
testcase_36 | AC | 269 ms
24,464 KB |
ソースコード
import sys sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 10**16 md = 998244353 # md = 10**9+7 class UnionFind: def __init__(self, n): self.state = [-1]*n self.size_table = [1]*n # cntはグループ数 self.cnt = n def root(self, u): stack = [] while self.state[u] >= 0: stack.append(u) u = self.state[u] for v in stack: self.state[v] = u return u def merge(self, u, v): u = self.root(u) v = self.root(v) if u == v: return du = -self.state[u] dv = -self.state[v] if du < dv: u, v = v, u if du == dv: self.state[u] -= 1 self.state[v] = u self.cnt -= 1 self.size_table[u] += self.size_table[v] return # グループの要素数 def size(self, u): return self.size_table[self.root(u)] mx=200005 # mx = 20 n = II() uf = UnionFind(mx) deg = [0]*mx st = set() u = -1 for i in range(n): h, w = LI() deg[h] -= 1 deg[w] += 1 uf.merge(h, w) st.add(h) st.add(w) u = h def findsg(): if uf.size(u) != len(st): return -2, -2 s = g = -1 for v in range(mx): if abs(deg[v]) > 1: return -2, -2 if deg[v] == 1: if s == -1: s = v else: return -2, -2 if deg[v] == -1: if g == -1: g = v else: return -2, -2 return s, g s, g = findsg() if s == -2: print(0) elif s == -1: print(len(st)) else: print(1)