結果
問題 | No.1640 簡単な色塗り |
ユーザー | 👑 rin204 |
提出日時 | 2022-01-26 16:19:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 584 ms / 2,000 ms |
コード長 | 2,078 bytes |
コンパイル時間 | 298 ms |
コンパイル使用メモリ | 81,772 KB |
実行使用メモリ | 101,532 KB |
最終ジャッジ日時 | 2024-12-23 07:21:29 |
合計ジャッジ時間 | 29,153 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 47 ms
52,352 KB |
testcase_01 | AC | 47 ms
52,224 KB |
testcase_02 | AC | 43 ms
52,352 KB |
testcase_03 | AC | 44 ms
52,352 KB |
testcase_04 | AC | 186 ms
96,916 KB |
testcase_05 | AC | 221 ms
100,096 KB |
testcase_06 | AC | 45 ms
52,224 KB |
testcase_07 | AC | 46 ms
52,352 KB |
testcase_08 | AC | 46 ms
52,096 KB |
testcase_09 | AC | 45 ms
52,224 KB |
testcase_10 | AC | 403 ms
92,160 KB |
testcase_11 | AC | 359 ms
87,932 KB |
testcase_12 | AC | 342 ms
87,552 KB |
testcase_13 | AC | 570 ms
96,932 KB |
testcase_14 | AC | 506 ms
96,776 KB |
testcase_15 | AC | 304 ms
84,916 KB |
testcase_16 | AC | 310 ms
85,120 KB |
testcase_17 | AC | 453 ms
94,568 KB |
testcase_18 | AC | 181 ms
78,336 KB |
testcase_19 | AC | 342 ms
87,600 KB |
testcase_20 | AC | 394 ms
90,700 KB |
testcase_21 | AC | 342 ms
87,240 KB |
testcase_22 | AC | 161 ms
78,080 KB |
testcase_23 | AC | 405 ms
92,288 KB |
testcase_24 | AC | 243 ms
80,732 KB |
testcase_25 | AC | 335 ms
87,744 KB |
testcase_26 | AC | 425 ms
92,488 KB |
testcase_27 | AC | 299 ms
83,900 KB |
testcase_28 | AC | 500 ms
97,920 KB |
testcase_29 | AC | 432 ms
92,288 KB |
testcase_30 | AC | 211 ms
80,564 KB |
testcase_31 | AC | 466 ms
97,024 KB |
testcase_32 | AC | 437 ms
93,824 KB |
testcase_33 | AC | 358 ms
89,344 KB |
testcase_34 | AC | 394 ms
92,040 KB |
testcase_35 | AC | 362 ms
88,780 KB |
testcase_36 | AC | 212 ms
80,516 KB |
testcase_37 | AC | 242 ms
81,280 KB |
testcase_38 | AC | 470 ms
95,364 KB |
testcase_39 | AC | 337 ms
85,360 KB |
testcase_40 | AC | 295 ms
84,992 KB |
testcase_41 | AC | 408 ms
92,316 KB |
testcase_42 | AC | 315 ms
86,316 KB |
testcase_43 | AC | 311 ms
86,400 KB |
testcase_44 | AC | 326 ms
86,884 KB |
testcase_45 | AC | 289 ms
84,224 KB |
testcase_46 | AC | 212 ms
80,796 KB |
testcase_47 | AC | 193 ms
79,312 KB |
testcase_48 | AC | 453 ms
95,360 KB |
testcase_49 | AC | 144 ms
77,696 KB |
testcase_50 | AC | 46 ms
52,352 KB |
testcase_51 | AC | 44 ms
52,096 KB |
testcase_52 | AC | 554 ms
101,144 KB |
testcase_53 | AC | 584 ms
101,532 KB |
07_evil_01.txt | AC | 987 ms
126,336 KB |
07_evil_02.txt | AC | 1,347 ms
148,680 KB |
ソースコード
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n self.hasloop = [False] * n self.group = n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: self.hasloop[x] = True return self.group -= 1 if self.parents[x] > self.parents[y]: x, y = y, x self.hasloop[x] |= self.hasloop[y] self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return self.group def all_group_members(self): dic = {r:[] for r in self.roots()} for i in range(self.n): dic[self.find(i)].append(i) return dic def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) n = int(input()) ab = [] UF = UnionFind(n) edges = [[] for _ in range(n)] ans = [-1] * n used = [False] * n stack = [] for i in range(n): a, b = map(int, input().split()) a -= 1 b -= 1 if UF.same(a, b): ans[i] = a + 1 stack.append(a) used[i] = True else: edges[a].append((b, i)) edges[b].append((a, i)) UF.union(a, b) for r in UF.roots(): if not UF.hasloop[r]: print("No") exit() print("Yes") while stack: pos = stack.pop() for npos, i in edges[pos]: if used[i]: continue used[i] = True ans[i] = npos + 1 stack.append(npos) print(*ans, sep="\n")