### choice's blog

By choice, 12 years ago,

### Problem A - IQ Test

We can store two values, countodd and counteven, as the number of odd or even elements in the series. We can also store lastodd and lasteven as the index of the last odd/even item encountered. If only one odd number appears --- output lastodd; otherwise only one even number appears, so output lasteven.

### Problem B - Telephone Numbers

There are many ways of separating the string into clusters of 2 or 3 characters. One easy way is to output 2 characters at a time, until you have only 2 or 3 characters remaining. Here is a possible C++ solution:

<code>
for( i=0; i<n; i++ )
{
putchar(buf[i]);
if( i%2 && i<n-(n%2)-2 ) putchar('-');
}
</code>

### Problem C - Roads in Berland

If you are familiar with the Floyd-Warshall algorithm, then this solution may be easier to see.

Initially, we are given a matrix D, where D[i][j] is the distance of shortest path between city i and city j. Suppose we build a new road between a and b with length shorter than D[a][b]. How do we update the rest of the graph accordingly?

Define a new matrix D', whose entries D'[i][j] are the minimum path distance between i and j while taking into account the new road ab. There are three possibilities for each i, j:
• D'[i][j] remains unchanged by the new road. In this case D'[i][j] = D[i][j]
• D'[i][j] is shorter if we use the new road ab. This means that the new path i, v1, v2, ..., vn, j must include the road a, b. If we connect the vertices i, a, b, j together in a path, then our new distance will be D[i][a] + length(ab) + D[b][j].
• Lastly, we may have to use the road ba. (Note that this may not be the same as road ab.) In this case, we have D'[i][j] = D[i][b] + length(ab) + D[a][j].
Thus, for each new road that we build, we must update each path i, j within the graph. Then we must sum shortest distances between cities. Updating the matrix and summing the total distance are both O(N2), so about 3002 operations. Lastly, there are at most 300 roads, so in total there are about 3003 operations.

One thing to note is that the sum of all shortest distances between cities may be larger than an int; thus, we need to use a long when calculating the sum.

### Problem D - Roads not only in Berland

Before we start this problem, it is helpful to know about the union find data structure. The main idea is this: given some elements x1, x2, x3, ..., xn that are partitioned in some way, we want to be able to do the following:
• merge any two sets together quickly
• find the parent set of any xi
This is a general data structure that sometimes appears in programming competitions. There are a lot of ways to implement it; one good example is written by Bruce Merry (aka BMerry) here.

Back to the problem: Every day we are allowed to build exactly 1 road, and close exactly 1 road. Thus, we can break the problem into two parts:
• How do we connect the parts of the graph that are disconnected?
• How do we remove roads in a way that does not disconnect parts of the graph?
Let build be the list all roads that need to be built, and let close be the list of nodes that need to be closed. We can show that in fact, these lists are of the same size. This is because the connected graph with n nodes is a tree if and only if it has n - 1 edges. Thus, if we remove more roads than than we build, then the graph is disconnected. Also, if we build more roads than we remove, then we have some unnecessary roads (the graph is no longer a tree).

Now consider the format of the input data:
a1, b1
a2, b2
...
an - 1, bn - 1
We can show that edge (ai, bi) is unnecessary if and only if the nodes ai, bi have already been connected by edges (a1, b1), (a2, b2), ..., (ai - 1, bi - 1). In other words, if the vertices ai, bi are in the same connected component before we, add (ai, bi) then we do not need to add (ai, bi). We can use union-find to help us solve this problem:

<code>
for( i from 1 to n-1 )
{
else merge(ai, bi);
}
</code>

In other words, we treat each connected component as a set. Union find allows us to find the connected component for each node. If the two connected components are the same, then our new edge is unnecessary. If they are different, then we can merge them together (with union find). This allows us to find the edges that we can remove.

In order to find the edges that we need to add to the graph, we can also use union-find: whenever we find a component that is disconnected from component 1, then we just add an edge between them.

<code>
for( i from 2 to n )
if( find(vi)!=find(v1) )
{
then merge(v1, vi);
}
</code>

We just need to store the lists of roads that are unnecessary, and the roads that need to be built.

### Problem E - Test

The way I solved this problem is with a hash function. Hash functions can fail on certain cases, so in fact, my solution is not 'correct'. However, it passed all the test cases =P

Let the input strings be s0, s1, s2. We can build the shortest solution by permuting the strings and then trying to 'attach' them to each other. I.e., we need to find the longest overlapping segments at the end of string a and the beginning of string b. The obvious brute force solution won't run in time. However, we can use a hash function to help us calculate the result in O(n) time, where n is min(len(a), len(b)). The hash function that I used was the polynomial hash(x0, x1, ..., xn) = x0 + ax1 + a2x2 + ... + anxn. This polynomial is a good hash function in this problem because it has the following useful property:
Given hash(xi, ..., xj), we can calculate the following values in O(1) time:
• hash(xi - 1, xi, ..., xj) = xi - 1 + a × hash(xi, ..., xj)
• hash(xi, ..., xj, xj + 1) = hash(xi, ..., xj) + aj + 1 - i × xj + 1
In other words, if we know the hash for some subsequence, we can calculate the hash for the subsequence and the previous element, or the subsequence and the next element. Given two strings a, b, we can calculate the hash functions starting from the end of a and starting from the beginning of b. If they are equal for length n, then that means that (maybe) a and b overlap by n characters.

Thus, we can try every permutation of s0, s1, s2, and try appending the strings to each other. There is one last case: if si is a substring of sj for some i ≠ j, then we can just ignore si. We can use hash functions to check that one string is contained within another one.

• +1

 11 years ago, # |   0 Thank you Choice, very good solutions :-)
 » 6 years ago, # |   0 I don't quite understand the problem statement in problem A. Or maybe I don't understand the editorial. What about test cases like 5 1 5 7 9 11 which should have the output as 1 but the AC solution gives 5. Also what about test cases like 5 5 9 13 15 21 which should give the output as 4 but gives 5. Please help me understand the problem. I have spent quite a lot time in this.
•  » » 3 years ago, # ^ |   0 Yeah, I also Don't understand the problem statement in problem A. Also I Don't understand the editorial of A.. Pls help..
•  » » » 3 years ago, # ^ |   0 Also I Don't understand the editorial of A Russian editorial is quite clear: there's only 1 odd or even number (1 even and e.g. 10 odd; 1 odd and e.g. 2 even). You have to find pos of it. My solution pass all tests: 50928772
•  » » » » 3 years ago, # ^ |   0 Yeah, I understand the problem, and got AC.. But I think the statement was quite unclear.. And Thanks a lot.. #MrReDox
•  » » » 3 years ago, # ^ |   0 The author especially didn't even bother explaining the "different in evenness".
 » 3 years ago, # | ← Rev. 2 →   +3 https://codeforces.com/contest/25/submission/58229465Task D. You can also use bfs for each connectivity component and find "useless" edges for each component. Now we have number of edges and number of components(they are equal). Now iterate for(int i = 1; i < components_count; ++i) build_road(component_vertex[0], component_vertex[i]); Where component_vertex is one of the vertexes for each connectivity component. Time complexity O(N). UPD: (O(V+E), but E = n — 1 and V = n, so O(V+E) = O(N + N — 1) = O(N))
 » 3 years ago, # |   0 Can we use Z algorithm in E ? Cause I got TLE on test 24[submission:58414744]
•  » » 23 months ago, # ^ |   0 KMP works fine. my_sub
 » 3 years ago, # |   0 For DAlternative solution: Keep track of nodes visited. Detect cycles by dfs-ing into each seperate tree (using nodes visited). Store each cyclic edge into an array. Then itterate through the array and connect each seperate tree to create one tree.
 » 2 years ago, # | ← Rev. 2 →   0 Soltuion for D. Just find self loops,multiple edges and cycles and remove all those edges. 89373449
•  » » 20 months ago, # ^ |   0 Can you please tell me where my code is wrongSolution link : https://codeforces.com/contest/25/submission/101586035