結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー rlangevinrlangevin
提出日時 2024-05-04 16:28:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 521 ms / 2,000 ms
コード長 3,143 bytes
コンパイル時間 357 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 139,360 KB
最終ジャッジ日時 2024-05-04 16:29:07
合計ジャッジ時間 10,603 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,992 KB
testcase_01 AC 38 ms
52,608 KB
testcase_02 AC 39 ms
52,608 KB
testcase_03 AC 73 ms
71,424 KB
testcase_04 AC 80 ms
76,672 KB
testcase_05 AC 72 ms
76,416 KB
testcase_06 AC 60 ms
69,760 KB
testcase_07 AC 379 ms
109,220 KB
testcase_08 AC 249 ms
94,140 KB
testcase_09 AC 280 ms
96,300 KB
testcase_10 AC 223 ms
92,388 KB
testcase_11 AC 508 ms
126,916 KB
testcase_12 AC 487 ms
126,592 KB
testcase_13 AC 462 ms
125,284 KB
testcase_14 AC 507 ms
125,660 KB
testcase_15 AC 515 ms
127,360 KB
testcase_16 AC 521 ms
127,104 KB
testcase_17 AC 512 ms
129,628 KB
testcase_18 AC 402 ms
139,360 KB
testcase_19 AC 412 ms
136,236 KB
testcase_20 AC 393 ms
125,792 KB
testcase_21 AC 448 ms
123,772 KB
20evil_special_uni1.txt AC 613 ms
128,512 KB
20evil_special_uni2.txt AC 462 ms
125,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0