Editorial: 2013 Asia Chengdu Regional Contest
Editorial for a virtual run of the 2013 ICPC Asia Chengdu Regional Contest, covering graph construction, string processing, dynamic programming, Aho-Corasick automata, and convolution.
Machine-translated from the Chinese original.

A
Problem
Construct a directed graph with vertices and edges, whose edges carry the weights respectively, such that
- there is at most one directed edge between any two vertices, and no self-loops
- every vertex can reach every vertex (including itself)
- the sum of weights along any directed cycle is a multiple of .
Solution
First put all vertices on one cycle, to satisfy condition . For , connect and with an edge of weight . For the edge between and , pick a weight from so that this cycle’s weight sum is a multiple of .
For the remaining edges, enumerate the pairs with distance , and assign the edges whose weight, taken mod , matches the distance mod between and .
B
Problem
Write a simple HTML code formatter.
- Leave the contents of a
tag(content wrapped in angle brackets<>) untouched - For
text(content not wrapped in angle brackets), strip redundant whitespace (ASCII 32space,ASCII 9tab, andASCII 10newline) so that words are separated by exactly one space. - Indent with spaces according to depth
Solution
Big simulation, lots of fiddly details. Spent an afternoon debugging it.
C
Problem
Given a permutation of , Little P wants to sort it in ascending order.
Solution
D
Problem
Given a directed graph with vertices and edges, Little P wants to travel from vertex to vertex within minutes.
Traversing each edge costs some time and money. Little P starts with yuan, and wants to have as much money as possible on hand when he reaches vertex .
Little P trades salt along the way. Every vertex except vertex and vertex has salt, at a given price. Each time he arrives at a vertex, he can
- sell a bag of salt
- buy a bag of salt
- do nothing
However, Little P can carry at most bags of salt at once (he starts with none). Trading salt takes no time.
Little P also has a device that lets him travel among parallel universes, labelled . He starts in universe . Each use of the device costs minute and moves him from universe to the vertex with the same label in universe .
The salt price at a vertex with the same label may differ across parallel universes, but the time and money cost of traversing the same edge is identical. Little P cannot visit vertex or vertex in universes .
Note: once he reaches vertex the journey ends. He must reach vertex within minutes, and the money on hand must never be negative during the journey.
Find the maximum amount of money he can have on hand when he reaches vertex .
Solution
This can be solved with a DP. Let denote the maximum amount of money when the time is , he is at vertex in universe , and he is carrying bags of salt.
Transitions are either walking one edge in the current universe, or moving to the next universe, in which case there are three cases to consider: buy salt / sell salt / do nothing.
E
F
Problem
Given an undirected graph with vertices and edges, where every edge has weight , decide whether the graph has a spanning tree whose edge weights sum to a Fibonacci number.
Solution
Compute the minimum spanning tree and the maximum spanning tree, and let their weight sums be and respectively. If there is a Fibonacci number in , the answer is Yes.
A rigorous proof is fairly involved, but it can be understood intuitively: starting from the minimum spanning tree, repeatedly remove an edge of weight and add an edge of weight while preserving the spanning-tree property, gradually transitioning to the maximum spanning tree.
G
Problem
Maintain a word list supporting two operations
- add a pattern string
- query the total number of occurrences of the pattern strings within a text string
The alphabet is , and the problem is forced online. If a pattern string occurs multiple times, assume each occurrence counts as a new match.
Solution
The first idea is an Aho-Corasick automaton, but an Aho-Corasick automaton cannot be modified. Consider keeping two Aho-Corasick automata . Let , keeping the node count of at and rebuilding it every time a new string is added; when the node count of exceeds , move the strings in into and rebuild . In the implementation, is simply fixed.
H
Problem
Operating systems and manufacturers compute disk space differently. An operating system takes , while a manufacturer takes . Given a string of the form 100[MB], find by what percentage the manufacturer’s computation falls short of the operating system’s, rounded to two decimal places.
Solution
Free points, just simulate it as stated. Note that when printing a % with printf, you have to write %%.
I
Problem
Simulate an ACM contest.
There are kinds of judge results
ERRORthe judge crashed; the team did not solve the problem, but incurs no penaltyNOthe code is wrong; the team did not solve the problem, and incurs a penaltyYESthe team solved the problem
To make the contest more tense and exciting, there is a scoreboard freeze mechanism.
- If a team has not solved a problem before the freeze, and submits it at or after the moment of the freeze, that problem becomes
frozenfor that team - Different teams may have different problems frozen
- For a
frozenproblem, the scoreboard only shows how many times that team submitted it, not the judge result
Rankings are determined by the following factors (ignoring frozen problems, from highest to lowest priority)
Solved, the number of problems solved; more solved ranks higherPenalty, letting be the number ofNOresults returned before the firstYES, and be the time of the firstYES, the penalty isLast Solved, the team whose last solved problem was solved earlier ranks higher; ties are broken by comparing the second-to-last solved problem, and so onName, teams are ranked by team name in descending lexicographic order; a lexicographically later name ranks higher
At the end of the contest, the scoreboard is unfrozen.
- Among teams that still have frozen problems, pick the one with the lowest rank on the board
Unfreezeone problem from that team’s frozen problems (if there are several, unfreeze the one whose name is lexicographically smallest). Reveal that problem’s judge result, recompute rankings, and update the board.- Repeat the above process until every team’s frozen problems have all been unfrozen.
- Obtain the final scoreboard.
Output the scoreboard before unfreezing, the final scoreboard, and the unfreezing process.
Solution
The first thing to solve is team ordering. Based on the problem statement, maintain the following information for each team
The (actual) state of each problem
unordered_map<char, bool>
where the state is only unsolved or solved.
Currently (publicly) solved problems
The scoreboard is sorted by this information
- Total penalty of solved problems,
penalty
set<int>
- Total count of solved problems,
size() - First AC time of each solved problem (note that due to the special unfreezing operation, time is not monotonic)
Penalty of each problem
unordered_map<char, int>
Once a problem is solved, it no longer accrues penalty.
The set of frozen problems
set<char>
Problems must be unfrozen in lexicographic order of problem letter.
Team name
The final tiebreaker.
J
Problem
Given two intervals , pick a random integer uniformly from and a random integer uniformly from . Find the probability that .
Solution
For convenience, first solve the case , then handle with a simple inclusion-exclusion.
contains integers, which can be split into full blocks of length , plus a partial block of length . Do the same for .
The contribution of full block against full block is (for each number in a full block of , there are matching numbers in the full blocks of ).
The contribution of full block against partial block is (each number in the partial block of can find matches in the full blocks of , and symmetrically for the partial block of ).
The contribution of partial block against partial block can be computed as the number of pairs summing to (enumerating starting from ).