結果

問題 No.2677 Minmax Independent Set
ユーザー rlangevinrlangevin
提出日時 2024-05-05 12:57:30
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,378 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 262,852 KB
最終ジャッジ日時 2024-05-05 12:58:43
合計ジャッジ時間 58,021 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,608 KB
testcase_01 AC 37 ms
52,864 KB
testcase_02 AC 41 ms
52,608 KB
testcase_03 AC 42 ms
52,992 KB
testcase_04 AC 42 ms
52,480 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1,691 ms
246,748 KB
testcase_08 WA -
testcase_09 AC 1,720 ms
246,512 KB
testcase_10 AC 1,528 ms
260,812 KB
testcase_11 AC 1,512 ms
260,968 KB
testcase_12 AC 1,304 ms
253,844 KB
testcase_13 WA -
testcase_14 AC 1,734 ms
256,956 KB
testcase_15 AC 1,845 ms
252,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1,107 ms
184,748 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 69 ms
70,784 KB
testcase_28 AC 1,432 ms
262,852 KB
testcase_29 AC 1,809 ms
250,312 KB
testcase_30 AC 39 ms
52,864 KB
testcase_31 AC 37 ms
52,992 KB
testcase_32 AC 39 ms
52,992 KB
testcase_33 AC 39 ms
53,248 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 586 ms
143,632 KB
testcase_51 AC 146 ms
81,940 KB
testcase_52 AC 1,205 ms
228,120 KB
testcase_53 AC 1,079 ms
216,740 KB
testcase_54 AC 144 ms
81,264 KB
testcase_55 AC 1,552 ms
252,972 KB
testcase_56 AC 1,459 ms
252,388 KB
testcase_57 AC 1,556 ms
249,360 KB
testcase_58 AC 1,469 ms
251,384 KB
testcase_59 AC 1,500 ms
249,928 KB
testcase_60 AC 1,000 ms
247,820 KB
testcase_61 AC 908 ms
241,792 KB
testcase_62 AC 928 ms
243,628 KB
testcase_63 WA -
権限があれば一括ダウンロードができます

ソースコード

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, 0]
        self.dp1 = [self.unit for _ in range(N)]
        self.dp2 = [self.unit for _ in range(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[0] + b[1], a[1] + max(b)]

    def calc_dp(self, v):
        return [v[0] + 1, v[1]]

    def calc_ans(self, v):
        return [v[0] + 1, 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 for _ in range(Nt + 1)]
                self.AcR[u] = [self.unit for _ in range(Nt + 1)]
                for i, (v, c) in enumerate(self.mem[u]):
                    temp = self.unit
                    self.AcL[u][i + 1] = self.merge(self.merge(temp, self.AcL[u][i]), self.cost(self.dp1[v], c))
                for i, (v, c) in enumerate(self.mem[u][::-1]):
                    temp = self.unit
                    self.AcR[u][i + 1] = self.merge(self.merge(temp, 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]):
                temp = self.unit
                self.dp2[v] = self.merge(self.merge(self.cost(self.merge(temp, 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][1]
            for u, c in self.G[i]:
                if self.par[i] == u:
                    continue
                ans[i] += self.cost(self.dp1[u][1], 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 = N + 1
for a in G.solve():
    ans = min(ans, a)
    
print(ans + 1)
0