This is probably not a proof, it is just my thinking during the contest

Suppose that you have two vertex with even degree (one is parent of other).

If you remove the parent, the child will be affected, you will need to remove some child to have even degree again... It's kinda hard to check this from parent to child (root to leaf).

Try to go in the opposite way: from leaf to root.

You have a leaf (obviously odd degree), how can you remove it? You need to remove it's parent... If the parent is odd, then you need to keep checking the parent until you reach a vertex with even degree. Now you remove it.

Then you have just disconnected the tree, the new tree should be solvable, otherwise "NO"

Otherwise, there is always at least a even-degree vertex. However, I don't know if it is true that there is always at least a even-degree vertex that seperate the tree into smaller component that has odd number of vertices (If it is true, just choose this vertices and solve the subproblem). I couldn't find a counterexample so I bet my luck in it, but failed miserably.

There will definitely be a vertex with even degree breaking the tree into odd components. We can first remove all even sized subtrees of the root. Once all even sized subtrees have been visited, print current vertex and then visit odd sized subtrees. In this method, we are considering either odd sized trees with no parent edge, or even sized trees with a parent edge, and you can prove using induction that this works :

You can remove the vertices greedily by its heights: remove the ones with greater height first.

If you are removing a vertex V, its subtree has no vertex X ≠ V with odd degree, otherwise it would be removed before V. The children of V will now have even degrees, since just one edge is removed from them, and you can apply the same idea to them, since their subtrees have no nodes with odd degree beside them.

So, all components that were on V's subtree are good to go; you just need to prove that the upper component is good to go too. That's easy: since the number of edges in V's subtree is even, then the parity of removed edges of the upper component did not change, which implies that there is another even-degree vertex to be removed.

Similarly, You can divide the whole series in (n + 1) / k terms which is a G.P with common ratio (bk / ak) and then use the formula for the sum of geometric progression (don't forget the case when common ratio of this G.P is 1).

Can anyone tell me if this solution for Div2.D is correct? I came upon it too late in the contest and didn't have time to implement it. So, here it goes:

Lets start at the leaves, it is obvious that their parent will have to be destroyed before them.

Now let's go 1 level up on the tree, if a parent of a leaf has even degree, then for sure its parent will have to be after before it, otherwise it wouldn't be possible to destroy it.

If it has an odd degree, then it will have to be destroyed before it. Now, lets substract the number of sons that will have to be clicked before their parent from the parent degree and continue doing the same procedure, going up the tree.

Lets make a directed graph, that has edges (u,v) for every u and v such that they are adjacent in the tree and u has to be clicked before v.

Do a topological sort on this graph and start clicking vertices in order, if at some step we have to click a vertex with odd degree, there is no solution. Otherwise, simply clicking in order will produce a solution.

If I had to guess, I'd say it's not a problem of time complexity, but of correctness.

In your submission, you write int t =k*n/2, but the multiplication can exceed the limits of the int type, causing undefined behavior. What I'm guessing is that at some point this t becomes negative and since you passed it as an exponent to your power function, it will cause an infinite loop.

Just a hunch though, I'm not at my laptop right now so I can't test :)

My solution for B: Maintain a set with all nodes with even degree ordered by depth first (distance to root). At each step, remove the deepest node (farthest from the root) and update degrees accordingly, removing or adding to the set as necessary.

My solution for D: Perform Suffix Array on S. Then for each query (t, k), all occurences of t in S will form a contiguous segment in the Suffix Array. Let the suffix array be SA, and suppose that the subarray for a given query in it is (l, r), then we need to find a set X of k indices i1, i2, ..., ik from the range (l, r) such that max(SA[i]) - min(SA[i]) is minimized (). I did that by pushing all SA[i] with l ≤ i ≤ r to a vector, then sorting the vector and checking all values of vi - vi - k + 1 for all i ≥ {k - 1}. I was certain this approach would get TLE in a very early test case. I can't believe such a thing passed systests....... but I'm really happy it did! :)

Your solution for D works in . Let's consider all strings of the same length. The total number of their occurrences in s is O(n), because at each position there can be at most one string. The number of defferent lengths is . So the number of all occurences is . You can also get rid of additional if you use Aho-Corasick instead of suffix array.

Always process the child nodes first before their parent (like a topological sorting where the directed edges go from the nodes to their parent). Initially start with the leaf nodes in the queue of the nodes to be processed. When processing a node, if its degree is even, delete the whole subtree rooted at it, and decrease the degree of its parent by 1. And after processing the node, look at its parent, if this parent has its children all processed then push this parent to the queue. At the end, you succeed if you have deleted the whole tree.

This idea is based on the fact that the information you start with is that the leaf nodes can't be removed unless their parents are removed. And if you processed the whole subtree under some parent and this parent still can't be removed you have to remove its parent, and so on. If you reached the root and couldn't remove it, so it is impossible to remove all nodes.

I see many people have provided greedy solutions for Div1B/Div2D. Here's my DP on tree solution (which ACed) for those interested:

First, observe that for any vertex u, having parent v, you can destroy the subtree of u either entirely before destroying vertex v or entirely after destroying vertex v. So, let's classify our vertices into two types: 'before' vertex — a vertex whose subtree is destroyed before destroying it's parent and 'after' vertex — a vertex whose subtree is destroyed after destroying it's parent.

Now, identifying vertices is easy. First, note that all leaves are 'after' vertices. They can only be deleted after their parent is deleted. Now, observe that a vertex is a 'after' vertex if and only if it has even number of children who are 'after' vertices, otherwise it is a 'before' vertex.

So, all vertices can be classified into 'after' and 'before' with a single DFS. The answer to our question is YES if the root is an 'after' vertex otherwise it is NO. Printing the solution is also easy, for printing the solution of the subtree of vertex u, print the solutions of all it's children who are 'before' vertices, then print u, then print the solutions of all 'after' vertices.

On div 1 Problem D test 22. The n is 80445 by assert, and the kind of lengths of mi is at least 325 by assert too. So Sum of length of all strings in input is at least 133420. That's illegal to the tips of the subject.

There really is a problem but I don't think that would be. The problem is that the problem mentioned "Sum of length of all strings in input doesn't exceed 10^5", the total length of query is less than or equal to 10^5, but including the string s, the total length is greater than 10^5. But I don't think it caused any trouble.

B: No clever tricks, just tree DP — is it possible to delete the subtree of v if the parent of v is deleted before v / after v? If we determine which sons of v should be deleted before v for both of these cases, the order of deleted vertices can be computed recursively (to delete the subtree of v, delete subtrees of sons deleted before v, delete v, delete subtrees of remaining sons). For the tree DP, if v has a son that can't be deleted in either case, the answer is no for both; if it has a son that can be deleted in both cases, the answer is yes for both, and otherwise, we know exactly which sons should be deleted before/after v.

C: If the answer is non-zero, the problem can be formulated this way: we have a complete edge-weighted bipartite graph (vertices are widths and heights of rectangles, edges are rectangles, weights are ci); count the number of ways to assign integer weights to the vertices in such a way that the weight of each edge is the product of weights of its endpoints. This can be solved independently for each prime factor — we're adding exponents instead of multiplying integers, everything is determined uniquely if we fix one exponent, and since these exponents are small, we can try all possibilities for one of them and check by bruteforce if it actually leads to a valid assignment. We only need to deal with prime factors of w.l.o.g. c1; afterwards, we're left with a graph that has c1 = 1, and need to check if the only possible assignment in this graph is valid. Time complexity: .

D: If we know all positions where a query string occurs in s, the answer for this query can be easily computed in linear time with no additional overhead. Actually, there are sufficiently few of these positions, so we can find them all. For a query string with length greater than sqrt (let's say 100), we can find them by KMP with overhead O(|s|); there are only sqrt of these strings. All query strings with length at most sqrt can be stuffed into a trie. Now, for each suffix of s, we can find a path in the trie corresponding to this suffix (with length at most sqrt, since that's the depth of the trie), and all query strings that end in vertices on this path occur on the position corresponding to this suffix. Time complexity: . You can get MLE or TLE, watch out for your trie implementation, try to put smaller strings in it or use a hashmap instead.

In KMP, it's also in DFS order, or in the order in which vertices were added. So you didn't answer the second part of my question: where in KMP do you need to think about which order (out of at least 3 possibilities) it should be?

You don't need to think of it in both Kmp and Aho-Corasick if you have proper images of how it works in your head. Both algo proven by induction over length, so you use order of lengthes in your implementation. That's just ridiculuous that you think that it is hard to choose between dfs and bfs in such a case.

To do division in modular arithmetic, you multiply the numerator with the modular inverse of the denominator. A modular inverse for number a under the modulo n is such a number b that . This number b exists if and only if gcd(a, n) = 1. By Euler's theorem, we can conclude that b = aφ (n) - 1. You can read more about it online:

There's no overflow. The fact that division doesn't work in modular arithmetic doesn't have anything to do with a programming limitation such as integer variables having max values and overflowing. It just doesn't work mathematically.