結果
問題 | No.1605 Matrix Shape |
ユーザー | rlangevin |
提出日時 | 2023-04-25 12:23:47 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,719 bytes |
コンパイル時間 | 144 ms |
コンパイル使用メモリ | 82,148 KB |
実行使用メモリ | 447,848 KB |
最終ジャッジ日時 | 2024-11-14 08:19:13 |
合計ジャッジ時間 | 22,178 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,644 KB |
testcase_01 | AC | 44 ms
55,328 KB |
testcase_02 | AC | 44 ms
54,988 KB |
testcase_03 | AC | 42 ms
54,572 KB |
testcase_04 | AC | 43 ms
54,232 KB |
testcase_05 | AC | 43 ms
54,564 KB |
testcase_06 | AC | 44 ms
55,464 KB |
testcase_07 | AC | 44 ms
54,648 KB |
testcase_08 | AC | 44 ms
54,620 KB |
testcase_09 | AC | 44 ms
55,456 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | AC | 43 ms
54,996 KB |
testcase_13 | AC | 43 ms
55,560 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | TLE | - |
testcase_20 | AC | 1,562 ms
254,756 KB |
testcase_21 | TLE | - |
testcase_22 | AC | 884 ms
172,344 KB |
testcase_23 | TLE | - |
testcase_24 | WA | - |
testcase_25 | AC | 296 ms
115,928 KB |
testcase_26 | AC | 300 ms
115,788 KB |
testcase_27 | WA | - |
testcase_28 | AC | 282 ms
114,224 KB |
testcase_29 | WA | - |
testcase_30 | AC | 1,078 ms
156,896 KB |
testcase_31 | WA | - |
testcase_32 | AC | 958 ms
164,548 KB |
testcase_33 | AC | 708 ms
132,188 KB |
testcase_34 | AC | 236 ms
135,636 KB |
testcase_35 | WA | - |
testcase_36 | WA | - |
ソースコード
import sys readline = sys.stdin.readline sys.setrecursionlimit(10**7) def scc(N, G, RG): order = [] used = [0]*N group = [None]*N def dfs(s): used[s] = 1 for t in G[s]: if not used[t]: dfs(t) order.append(s) def rdfs(s, col): group[s] = col used[s] = 1 for t in RG[s]: if not used[t]: rdfs(t, col) for i in range(N): if not used[i]: dfs(i) used = [0]*N label = 0 for s in reversed(order): if not used[s]: rdfs(s, label) label += 1 return label, group # 縮約後のグラフを構築 def construct(N, G, label, group): G0 = [set() for i in range(label)] GP = [[] for i in range(label)] for v in range(N): lbs = group[v] for w in G[v]: lbt = group[w] if lbs == lbt: continue G0[lbs].add(lbt) GP[lbs].append(v) return G0, GP from bisect import * from copy import deepcopy def compress(lst): """ B: lstを座圧したリスト D: 元の値からindexを取得する辞書 """ B = [] D = dict() vals = deepcopy(lst) vals = list(set(vals)) vals.sort() for i in range(len(lst)): ind = bisect_left(vals, lst[i]) B.append(ind) for i in range(len(B)): D[lst[i]] = B[i] return B, D class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: x, y = y, x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] N = int(readline()) H, W = [0] * N, [0] * N for i in range(N): H[i], W[i] = map(int, readline().split()) D, _ = compress(H + W) M = max(D) + 1 G = [[] for i in range(M)] RG = [[] for i in range(M)] U = UnionFind(M) for i in range(N): G[D[i]].append(D[i + N]) RG[D[i + N]].append(D[i]) U.union(D[i], D[i + N]) if U.get_size(0) != M: print(0) exit() label, group = scc(M, G, RG) G0, GP = construct(M, G, label, group) print(len(GP[0]))