Official

D - 接続規格と変換器 / Connection Standards and Converters Editorial by admin Editorial for printing

claude5.5opus-high

Overview

We assign either \(0\) or \(1\) to each vertex of a tree. The problem asks to minimize the total sum of the assignment costs for each vertex and the costs of edges whose endpoints have different values. This can be solved using standard tree DP.

Analysis

  • Drawback of the naive approach: There are \(2^N\) combinations of standards for all vertices. With \(N \leq 200000\), a brute-force search will not finish in time at all.
  • Key insight: Since the graph is a tree, the subtree rooted at a vertex \(v\) is connected to its parent only by a single edge between \(v\) and its parent. Therefore, once the standard for \(v\) is determined, the optimal choice within the subtree can be determined independently of the outside.
  • Thus, we maintain states representing “the minimum cost in the subtree when vertex \(v\) has standard \(0\) / \(1\)”. We can compute this efficiently by aggregating information from children to their parents.
  • Concrete Example: Suppose vertex 1 (parent) and vertex 2 (child) are connected by an edge with cost \(C=3\), and \((A_1,B_1)=(5,1)\), \((A_2,B_2)=(0,10)\).
    • For vertex 2, the values are \(dp_0=0\) and \(dp_1=10\).
    • When vertex 1 has standard \(0\), the choices for the child are “child is also \(0\) with cost \(0\)” or “child is \(1\) with cost \(10+3=13\)”. Taking the smaller value \(0\), we get \(5+0=5\).
    • When vertex 1 has standard \(1\), the choices for the child are “child is also \(1\) with cost \(10\)” or “child is \(0\) with cost \(0+3=3\)”. Taking the smaller value \(3\), we get \(1+3=4\).
    • The answer is \(\min(5,4)=4\).

Algorithm

We perform tree DP. For the subtree rooted at vertex \(v\), define the following two states: - \(dp_0[v]\): The minimum cost within the subtree when \(v\) is assigned standard \(0\) - \(dp_1[v]\): The minimum cost within the subtree when \(v\) is assigned standard \(1\)

The initial values are \(dp_0[v]=A_v\) and \(dp_1[v]=B_v\). For each child \(w\) (with edge cost \(c\)), we accumulate the values using the following transitions:

\[dp_0[v] \mathrel{+}= \min(dp_0[w],\ dp_1[w]+c)\]

\[dp_1[v] \mathrel{+}= \min(dp_1[w],\ dp_0[w]+c)\]

Finally, the answer is \(\min(dp_0[r], dp_1[r])\) for the root \(r\).

The processing order is as follows: 1. Perform a BFS rooted at vertex \(0\), recording the traversal order order, the parent of each vertex par, and the edge cost to the parent pc. 2. Iterate through order in reverse. By adding each vertex’s value to its parent, children are guaranteed to be resolved before their parents.

Complexity

  • Time Complexity: \(O(N)\)
  • Space Complexity: \(O(N)\)

Implementation Points

  • Avoid recursion: Since \(N\) can be up to \(200000\), a recursive DFS could exceed Python’s recursion limit or cause a stack overflow. By traversing the BFS visit order in reverse, we achieve a bottom-up (child to parent) order non-recursively.

  • Fast I/O: The entire input is read at once using sys.stdin.buffer.read().split().

  • Case where \(N=1\): Since there are no edges, the reverse loop will not execute, and \(\min(A_1,B_1)\) is output directly.

  • Magnitude of values: The total can reach up to around \(4\times10^{14}\). This is fine with Python’s arbitrarily large integers, but in other languages, 64-bit integers must be used.

  • Constant-factor optimization: In the loop, a ternary operator is used instead of the min function.

    Source Code

import sys
def main():
    data=sys.stdin.buffer.read().split()
    n=int(data[0])
    A=[0]*n;B=[0]*n
    p=1
    for i in range(n):
        A[i]=int(data[p]);B[i]=int(data[p+1]);p+=2
    adj=[[] for _ in range(n)]
    for j in range(n-1):
        u=int(data[p])-1;v=int(data[p+1])-1;c=int(data[p+2]);p+=3
        adj[u].append((v,c));adj[v].append((u,c))
    par=[-1]*n
    pc=[0]*n
    order=[0]
    seen=[False]*n
    seen[0]=True
    i=0
    while i<len(order):
        v=order[i];i+=1
        for w,c in adj[v]:
            if not seen[w]:
                seen[w]=True
                par[w]=v
                pc[w]=c
                order.append(w)
    d0=A[:];d1=B[:]
    for k in range(n-1,0,-1):
        v=order[k]
        pa=par[v];c=pc[v]
        x=d0[v];y=d1[v]
        a=y+c
        d0[pa]+= x if x<a else a
        b=x+c
        d1[pa]+= y if y<b else b
    print(min(d0[0],d1[0]))
main()

This editorial was generated by claude5.5opus-high.

posted:
last update: