結果
問題 | No.768 Tapris and Noel play the game on Treeone |
ユーザー | rlangevin |
提出日時 | 2024-05-04 16:28:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 545 ms / 2,000 ms |
コード長 | 3,143 bytes |
コンパイル時間 | 259 ms |
コンパイル使用メモリ | 82,324 KB |
実行使用メモリ | 139,068 KB |
最終ジャッジ日時 | 2024-11-26 05:12:20 |
合計ジャッジ時間 | 10,232 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
53,540 KB |
testcase_01 | AC | 36 ms
54,544 KB |
testcase_02 | AC | 37 ms
54,096 KB |
testcase_03 | AC | 67 ms
71,704 KB |
testcase_04 | AC | 80 ms
76,460 KB |
testcase_05 | AC | 78 ms
76,296 KB |
testcase_06 | AC | 61 ms
70,832 KB |
testcase_07 | AC | 375 ms
109,088 KB |
testcase_08 | AC | 245 ms
93,624 KB |
testcase_09 | AC | 264 ms
96,560 KB |
testcase_10 | AC | 233 ms
92,016 KB |
testcase_11 | AC | 528 ms
126,784 KB |
testcase_12 | AC | 515 ms
126,132 KB |
testcase_13 | AC | 475 ms
125,284 KB |
testcase_14 | AC | 484 ms
125,660 KB |
testcase_15 | AC | 513 ms
126,932 KB |
testcase_16 | AC | 524 ms
127,284 KB |
testcase_17 | AC | 545 ms
129,404 KB |
testcase_18 | AC | 379 ms
139,068 KB |
testcase_19 | AC | 380 ms
136,112 KB |
testcase_20 | AC | 368 ms
125,540 KB |
testcase_21 | AC | 375 ms
123,264 KB |
20evil_special_uni1.txt | AC | 482 ms
128,792 KB |
20evil_special_uni2.txt | AC | 476 ms
125,832 KB |
ソースコード
class ReRooting(): def __init__(self, N=1, debug=0): self.N = N self.G = [[] for i in range(N)] self.mem = [[] for i in range(N)] self.AcL = [None] * N self.AcR = [None] * N self.par = [-1] * N self.sz = [1] * N self.depth = [0] * N self.unit = 0 self.dp1 = [self.unit] * N self.dp2 = [self.unit] * N self.debug = debug def add_edge(self, i=0, j=0, c=1): self.G[i].append((j, c)) self.G[j].append((i, c)) def merge(self, a, b): return a | b def calc_dp(self, v): return v ^ 1 def calc_ans(self, v): return v ^ 1 def cost(self, x, c): return x def bottom_up(self, s): stack = [s] while stack: u = stack.pop() if u >= 0: stack.append(~u) for v, c in self.G[u]: if v == self.par[u]: continue self.par[v] = u self.depth[v] = self.depth[u] + 1 stack.append(v) else: u = ~u self.mem[u] = [] for v, c in self.G[u]: if v == self.par[u]: continue self.sz[u] += self.sz[v] self.dp1[u] = self.merge(self.dp1[u], self.cost(self.dp1[v], c)) self.mem[u].append((v, c)) self.dp1[u] = self.calc_dp(self.dp1[u]) Nt = len(self.mem[u]) self.AcL[u] = [self.unit] * (Nt + 1) self.AcR[u] = [self.unit] * (Nt + 1) for i, (v, c) in enumerate(self.mem[u]): self.AcL[u][i + 1] = self.merge(self.AcL[u][i], self.cost(self.dp1[v], c)) for i, (v, c) in enumerate(self.mem[u][::-1]): self.AcR[u][i + 1] = self.merge(self.AcR[u][i], self.cost(self.dp1[v], c)) def top_down(self, s): stack = [s] while stack: u = stack.pop() for i, (v, c) in enumerate(self.mem[u]): self.dp2[v] = self.merge(self.merge(self.cost(self.dp2[u], c), self.AcL[u][i]), self.AcR[u][len(self.mem[u]) - i - 1]) self.dp2[v] = self.calc_dp(self.dp2[v]) stack.append(v) def solve(self, s=0): self.bottom_up(s) self.top_down(s) if self.debug: print('dp1', self.dp1) print('dp2', self.dp2) ans = [None] * self.N for i in range(self.N): ans[i] = self.dp2[i] for u, c in self.G[i]: if self.par[i] == u: continue ans[i] = self.merge(ans[i], self.cost(self.dp1[u], c)) ans[i] = self.calc_ans(ans[i]) if self.debug: print('ans', ans) return ans N = int(input()) G = ReRooting(N) for i in range(N - 1): u, v = map(int, input().split()) u, v = u - 1, v - 1 G.add_edge(u, v) ans = G.solve() print(ans.count(1)) for i in range(N): if ans[i]: print(i + 1)