結果
問題 | No.1640 簡単な色塗り |
ユーザー | rlangevin |
提出日時 | 2023-12-25 20:50:04 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 512 ms / 2,000 ms |
コード長 | 1,738 bytes |
コンパイル時間 | 362 ms |
コンパイル使用メモリ | 82,248 KB |
実行使用メモリ | 153,460 KB |
最終ジャッジ日時 | 2024-09-27 14:40:48 |
合計ジャッジ時間 | 23,646 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,272 KB |
testcase_01 | AC | 43 ms
54,528 KB |
testcase_02 | AC | 41 ms
54,144 KB |
testcase_03 | AC | 42 ms
54,400 KB |
testcase_04 | AC | 219 ms
153,460 KB |
testcase_05 | AC | 152 ms
99,200 KB |
testcase_06 | AC | 42 ms
53,760 KB |
testcase_07 | AC | 42 ms
54,272 KB |
testcase_08 | AC | 42 ms
54,016 KB |
testcase_09 | AC | 43 ms
54,016 KB |
testcase_10 | AC | 335 ms
92,116 KB |
testcase_11 | AC | 293 ms
89,812 KB |
testcase_12 | AC | 294 ms
89,044 KB |
testcase_13 | AC | 405 ms
100,084 KB |
testcase_14 | AC | 408 ms
99,848 KB |
testcase_15 | AC | 260 ms
86,020 KB |
testcase_16 | AC | 273 ms
87,004 KB |
testcase_17 | AC | 381 ms
96,032 KB |
testcase_18 | AC | 151 ms
79,276 KB |
testcase_19 | AC | 291 ms
89,032 KB |
testcase_20 | AC | 328 ms
91,956 KB |
testcase_21 | AC | 291 ms
89,136 KB |
testcase_22 | AC | 131 ms
78,976 KB |
testcase_23 | AC | 333 ms
93,612 KB |
testcase_24 | AC | 195 ms
82,712 KB |
testcase_25 | AC | 280 ms
89,132 KB |
testcase_26 | AC | 347 ms
94,872 KB |
testcase_27 | AC | 248 ms
85,508 KB |
testcase_28 | AC | 424 ms
98,992 KB |
testcase_29 | AC | 371 ms
94,976 KB |
testcase_30 | AC | 186 ms
81,392 KB |
testcase_31 | AC | 424 ms
101,180 KB |
testcase_32 | AC | 365 ms
98,496 KB |
testcase_33 | AC | 308 ms
91,748 KB |
testcase_34 | AC | 360 ms
95,060 KB |
testcase_35 | AC | 309 ms
91,724 KB |
testcase_36 | AC | 180 ms
81,516 KB |
testcase_37 | AC | 205 ms
82,860 KB |
testcase_38 | AC | 376 ms
99,020 KB |
testcase_39 | AC | 260 ms
86,636 KB |
testcase_40 | AC | 266 ms
87,592 KB |
testcase_41 | AC | 381 ms
96,032 KB |
testcase_42 | AC | 272 ms
89,332 KB |
testcase_43 | AC | 281 ms
89,032 KB |
testcase_44 | AC | 257 ms
88,668 KB |
testcase_45 | AC | 246 ms
85,672 KB |
testcase_46 | AC | 194 ms
81,520 KB |
testcase_47 | AC | 166 ms
80,652 KB |
testcase_48 | AC | 398 ms
100,020 KB |
testcase_49 | AC | 126 ms
78,208 KB |
testcase_50 | AC | 40 ms
54,272 KB |
testcase_51 | AC | 42 ms
53,888 KB |
testcase_52 | AC | 472 ms
114,712 KB |
testcase_53 | AC | 512 ms
108,084 KB |
07_evil_01.txt | AC | 778 ms
127,000 KB |
07_evil_02.txt | AC | 1,099 ms
149,808 KB |
ソースコード
import sys input = sys.stdin.readline from collections import * 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(input()) A, B = [0] * N, [0] * N G = [[] for i in range(N)] U = UnionFind(N) Q = [] cnt = [0] * N ans, seen = [-1] * N, [0] * N for i in range(N): A[i], B[i] = map(int, input().split()) A[i], B[i] = A[i] - 1, B[i] - 1 if not U.is_same(A[i], B[i]): G[A[i]].append((B[i], i)) G[B[i]].append((A[i], i)) else: Q.append(deque([A[i]])) seen[A[i]] = 1 ans[i] = A[i] + 1 U.union(A[i], B[i]) for i in range(N): ui = U.find(A[i]) cnt[ui] += 1 if cnt[ui] > U.get_size(ui): print("No") exit() for qs in Q: while qs: u = qs.popleft() for v, ind in G[u]: if seen[v]: continue ans[ind] = v + 1 qs.append(v) seen[v] = 1 print("Yes") for a in ans: print(a)