結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,120 KB
testcase_01 AC 43 ms
52,608 KB
testcase_02 AC 43 ms
52,608 KB
testcase_03 AC 43 ms
52,480 KB
testcase_04 AC 44 ms
52,736 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1,884 ms
246,236 KB
testcase_08 WA -
testcase_09 AC 1,870 ms
246,484 KB
testcase_10 AC 1,758 ms
260,304 KB
testcase_11 AC 1,771 ms
261,116 KB
testcase_12 AC 1,499 ms
253,840 KB
testcase_13 TLE -
testcase_14 AC 1,898 ms
257,192 KB
testcase_15 TLE -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1,222 ms
184,488 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 77 ms
70,656 KB
testcase_28 AC 1,636 ms
262,460 KB
testcase_29 AC 1,917 ms
250,316 KB
testcase_30 AC 44 ms
52,352 KB
testcase_31 AC 43 ms
52,608 KB
testcase_32 AC 44 ms
52,992 KB
testcase_33 AC 44 ms
53,376 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 672 ms
143,636 KB
testcase_51 AC 172 ms
81,680 KB
testcase_52 AC 1,336 ms
227,864 KB
testcase_53 AC 1,241 ms
216,488 KB
testcase_54 AC 170 ms
81,004 KB
testcase_55 AC 1,688 ms
252,716 KB
testcase_56 AC 1,692 ms
252,448 KB
testcase_57 AC 1,651 ms
249,868 KB
testcase_58 AC 1,938 ms
251,560 KB
testcase_59 TLE -
testcase_60 AC 1,196 ms
247,616 KB
testcase_61 AC 1,103 ms
241,656 KB
testcase_62 AC 1,188 ms
243,372 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