結果
問題 | No.1640 簡単な色塗り |
ユーザー | brthyyjp |
提出日時 | 2022-01-04 19:00:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 695 ms / 2,000 ms |
コード長 | 1,749 bytes |
コンパイル時間 | 495 ms |
コンパイル使用メモリ | 81,912 KB |
実行使用メモリ | 111,692 KB |
最終ジャッジ日時 | 2024-10-14 14:43:08 |
合計ジャッジ時間 | 27,156 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,696 KB |
testcase_01 | AC | 43 ms
55,412 KB |
testcase_02 | AC | 43 ms
55,136 KB |
testcase_03 | AC | 43 ms
55,040 KB |
testcase_04 | AC | 151 ms
108,932 KB |
testcase_05 | AC | 165 ms
106,924 KB |
testcase_06 | AC | 43 ms
55,740 KB |
testcase_07 | AC | 43 ms
54,864 KB |
testcase_08 | AC | 43 ms
54,528 KB |
testcase_09 | AC | 43 ms
55,296 KB |
testcase_10 | AC | 426 ms
95,732 KB |
testcase_11 | AC | 395 ms
93,760 KB |
testcase_12 | AC | 378 ms
92,384 KB |
testcase_13 | AC | 543 ms
106,168 KB |
testcase_14 | AC | 524 ms
105,204 KB |
testcase_15 | AC | 331 ms
87,964 KB |
testcase_16 | AC | 346 ms
89,356 KB |
testcase_17 | AC | 472 ms
101,452 KB |
testcase_18 | AC | 177 ms
79,080 KB |
testcase_19 | AC | 366 ms
91,368 KB |
testcase_20 | AC | 408 ms
96,864 KB |
testcase_21 | AC | 397 ms
94,008 KB |
testcase_22 | AC | 161 ms
78,484 KB |
testcase_23 | AC | 440 ms
96,332 KB |
testcase_24 | AC | 259 ms
83,564 KB |
testcase_25 | AC | 364 ms
90,824 KB |
testcase_26 | AC | 447 ms
98,948 KB |
testcase_27 | AC | 308 ms
87,780 KB |
testcase_28 | AC | 511 ms
104,244 KB |
testcase_29 | AC | 444 ms
98,700 KB |
testcase_30 | AC | 145 ms
77,912 KB |
testcase_31 | AC | 283 ms
86,136 KB |
testcase_32 | AC | 263 ms
85,144 KB |
testcase_33 | AC | 225 ms
82,476 KB |
testcase_34 | AC | 245 ms
83,312 KB |
testcase_35 | AC | 227 ms
82,384 KB |
testcase_36 | AC | 142 ms
77,948 KB |
testcase_37 | AC | 162 ms
78,576 KB |
testcase_38 | AC | 268 ms
85,000 KB |
testcase_39 | AC | 190 ms
80,680 KB |
testcase_40 | AC | 193 ms
80,712 KB |
testcase_41 | AC | 251 ms
84,132 KB |
testcase_42 | AC | 209 ms
81,108 KB |
testcase_43 | AC | 207 ms
81,572 KB |
testcase_44 | AC | 207 ms
81,280 KB |
testcase_45 | AC | 193 ms
80,488 KB |
testcase_46 | AC | 145 ms
77,980 KB |
testcase_47 | AC | 128 ms
77,728 KB |
testcase_48 | AC | 275 ms
85,440 KB |
testcase_49 | AC | 97 ms
76,928 KB |
testcase_50 | AC | 44 ms
54,044 KB |
testcase_51 | AC | 43 ms
54,536 KB |
testcase_52 | AC | 695 ms
111,692 KB |
testcase_53 | AC | 690 ms
111,108 KB |
07_evil_01.txt | AC | 995 ms
143,456 KB |
07_evil_02.txt | AC | 1,355 ms
179,096 KB |
ソースコード
class UnionFind: def __init__(self, n): self.par = [-1]*n self.rank = [0]*n def Find(self, x): if self.par[x] < 0: return x else: self.par[x] = self.Find(self.par[x]) return self.par[x] def Unite(self, x, y): x = self.Find(x) y = self.Find(y) if x != y: if self.rank[x] < self.rank[y]: self.par[y] += self.par[x] self.par[x] = y else: self.par[x] += self.par[y] self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 def Same(self, x, y): return self.Find(x) == self.Find(y) def Size(self, x): return -self.par[self.Find(x)] import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import Counter, deque, defaultdict n = int(input()) uf = UnionFind(n) E = [] for i in range(n): a, b = map(int, input().split()) a, b = a-1, b-1 uf.Unite(a, b) E.append((a, b)) C = Counter() for a, b in E: C[uf.Find(a)] += 1 for k, v in C.items(): if v != uf.Size(k): print('No') exit() uf = UnionFind(n) stack = [] ans = [-1]*n g = [[] for i in range(n)] visit = [False]*n for i, (a, b) in enumerate(E): if uf.Same(a, b): stack.append(a) ans[i] = a visit[a] = True else: uf.Unite(a, b) g[a].append((b, i)) g[b].append((a, i)) while stack: v = stack.pop() for u, i in g[v]: if visit[u]: continue ans[i] = u stack.append(u) visit[u] = True print('Yes') ans = [i+1 for i in ans] print(*ans, sep='\n')