Tutorial
Tutorial
• It passes a centroid c.
• It does not pass a centroid c but uses some edges that connect different subtrees of c.
The first case can be solved easily by computing a single-source shortest path from c, using Dijkstra’s
algorithm.
For the second case, the edges that connect different subtrees of c are ring roads, and you can observe
that they are at most the number of c’s children. This fact is obviously true for the original tree. For
the subproblems, note that you can add at most one edge that the graph is still a tree where each leaf
is augmented with ring roads. Therefore, there are at most 3 edges that connect different subtrees of c.
Passing an edge means passing one endpoint of each edge, so you can compute 3 single-source shortest
path from any arbitrary endpoint of such edges.
The third case only holds if the queried vertices belong to the same subproblem - proceed recursively.
Each queries belong to at most O(log N ) subproblems, and for each layer you call Dijkstra’s algorithm 4
times, which takes O(N log N ) time. As a result, we obtain an O(N log2 N + Q log N )-time algorithm.
To find the relevant ring roads in the second case, simple DFS is sufficient, and you don’t need any analysis
of the specific structure of the given graph.
tree. Naively, you can scan all tree edges where both endpoints belong to S and compute the components
with graph search or disjoint sets, which requires O(N ) time per query and times out.
Performing DFS on the vertices of S may seem to work, but this approach may encounter many irrelevant
edges, hence this also times out.
To improve the algorithm, root the tree arbitrarily, and compute the parent for all non-root vertices. An
edge is relevant if and only if par(v) ∈ S, v ∈ S, therefore you can simply
Piterate through S and check if
par(v) ∈ S with a simple boolean array. The time complexity is O(N + Ki ).
• max |Xi | − 1 is called treewidth (We have to compute width-3 tree decomposition).
Page 1 of 6
44th Petrozavodsk Programming Camp, Winter 2023
Day 4: KAIST+KOI Contest, Grand Prix of Korea, March 12, 2023
Let’s assume that the input is a binary tree. If we ignore the ring road, we can construct a trivial
decomposition:
It’s easy to verify that the decomposition is valid, and has width 1. The tree is binary, so supporting the
ring road is simple: For each ring road (u, v), add the element u over the path between u and v. This
modification yields a solution of width 4.
To reduce the width by one, we appropriately subdivide the edges of the skeleton tree and spread the
element of bags. Let df s(v) be the recursive function such that:
For some child (w1 , w2 ) of v, the return value of df s(w1 ), df s(w2 ) will be (la , w1 , lb ), (lb , w2 , lc ), respectively.
We can merge them in a following way:
We need 4 new nodes for merging two trees, hence the construction yields a skeleton tree of size at most
4N . Generalizing this to a non-binary tree is trivial.
Remark. As noted, the tree decomposition of a graph makes tree-specific techniques applicable to general
graphs. For example, problem A can be solved by constructing a tree decomposition as in this problem,
and by applying your favorite way of computing distance in a tree (centroid decomposition or sparse
table). Of course, it’s much easier to use ad-hoc approaches to problem A, but author knows at least five
problems that can be described as "find tree decomposition, and copy-paste distance computing template
in bounded treewidth graph"(For example, NEERC 2015 C, GCJ 2020 R2D, JOI Spring Camp 2017
D2P3).
for h < hi
D[i − 1, k, h] + 1
D[i, k, h] = min(DP [i − 1, k, h], minh0 <h D[i − 1, k − 1, h0 ]) for h = hi
for h > hi
D[i − 1, k, h]
Page 2 of 6
44th Petrozavodsk Programming Camp, Winter 2023
Day 4: KAIST+KOI Contest, Grand Prix of Korea, March 12, 2023
This takes O(N 2 K) naively, so we have to do it efficiently. Suppose we have added buildings 1 . . . i so far.
We will store K arrays D[i, k, ∗] for k = 1 . . . K in K segment trees. Now, the task of adding a building
to the set becomes adding 1 to a range, querying the minimum in a range, and updating a point in a
segment tree. Each task can be done in O(log N ) per segment tree using lazy propagation. Since there
are K segment trees and we will add N buildings in total, the time complexity is O(N K log N ).
• Z =Y.
• I(j) ≥ j
Page 3 of 6
44th Petrozavodsk Programming Camp, Winter 2023
Day 4: KAIST+KOI Contest, Grand Prix of Korea, March 12, 2023
i.e. I(j) is the maximum index such that Nj∼I(j) is non-increasing sequence.
It can be checked that i ≤ j for given index j with following steps.
Each step can be proceed in the node of segment tree where the node of segment tree is consists of count
of each digit in each interval, l to r and l to min{I(l), r}.
Time complexity is O(D(N + Q log N )) where D is the number of digits.
Page 4 of 6
44th Petrozavodsk Programming Camp, Winter 2023
Day 4: KAIST+KOI Contest, Grand Prix of Korea, March 12, 2023
Note that the game has to end in finite turns because each move reduces the sum of xi . It means that
your opponent cannot indefinitely stall the game by always increasing the heap size.
When K > 1, the game is equivalent to playing K distinct Nim games, which is just a single Nim game
with all the heaps from each game.
Page 5 of 6
44th Petrozavodsk Programming Camp, Winter 2023
Day 4: KAIST+KOI Contest, Grand Prix of Korea, March 12, 2023
1. Create one source vertex, one sink vertex, one vertex for each room, and one vertex for each places
where we can put windows
2. Make edges connecting source vertex and rooms and give it capacity according to number of windows
needed and cost zero.
3. Make edges connecting rooms and potential window locations. Give the edge capacity one and cost
zero.
4. For each potential window location, connect it and the sink vertex twice. The first edge has capacity
one and cost zero and the second one has capacity one and cost equal to the discomfort we get when
we put two windows in this location (i.e. product of numbers of people in two rooms connected to
this window location)
And now running MCMF algorithm on this graph solves the problem. The time complexity is O(n2 m2 ).
Page 6 of 6