TreeColoring
SRM 624 · 2013-12-22 · by ivan_metelsky
Problem Statement
Please note that this problem has a non-standard time limit: 4 seconds.
Dimas is very fond of trees, he really enjoys solving problems on trees. Recently, his professor Rohit gave him a very difficult task to solve. This is it:
You're given a tree on N vertices. (A tree is an undirected connected graph with no cycles.) Different edges of the tree may have different lengths. Initially, all vertices are white. You now have to process Q queries. There are two types of queries:
- type 1: Given a node x, paint it blue.
- type 2: Given a node x, compute the sum of all distances between x and a blue node.
You are given the
int curValue = startSeed;
int genNextRandom() {
curValue = (curValue * 1999 + 17) % 1000003;
return curValue;
}
void generateInput() {
for (int i = 0; i < N-1; i++) {
distance[i] = genNextRandom() % maxDist;
parent[i] = genNextRandom();
if (parent[i] < threshold) {
parent[i] = i;
} else {
parent[i] = parent[i] % (i + 1);
}
}
for (int i = 0; i < Q; i++) {
queryType[i] = genNextRandom() % 2 + 1;
queryNode[i] = genNextRandom() % N;
}
}
The output of the above pseudocode are four arrays: parent, distance, queryType, and queryNode.
The arrays parent and distance have N-1 elements each. For each valid i, our tree contains an edge between the vertices (i+1) and parent[i]. The length of that edge is distance[i]. Note that parent[i] will always be between 0 and i, inclusive.
The arrays queryType and queryNode have Q elements each. For each valid i, the i-th query (0-based index) you should process has the type queryType[i], and should be applied to the vertex queryNode[i]. The queries must be processed in the given order.
Return the bitwise XOR of the answers to all type 2 queries.
Notes
- The intended solution does not rely on any properties of the tree and queries generator provided in the problem statement. It can process 100,000 queries on any tree containing up to 100,000 vertices. It is also able to calculate individual answers to each type 2 query (not just bitwise XOR of all answers).
Constraints
- N will be between 2 and 100,000, inclusive.
- Q will be between 1 and 100,000, inclusive.
- startSeed will be between 0 and 1,000,002, inclusive.
- threshold will be between 0 and 1,000,003, inclusive.
- maxDist will be between 1 and 1,000,003, inclusive.
4 6 15 2 5 Returns: 7
parent = {0,1,2} distance = {2,1,3} queryType = {2,1,2,2,2,1} queryNode = {2,3,2,3,1,3} Here are our responses to the 6 queries: There are no blue nodes so the answer is clearly zero. We paint the node #3 blue. The distance between node #2 and node #3 is 3. The distance between node #3 and itself is 0. The distance between node #1 and node #3 is 4. As the node #3 is already blue, we just ignore this query.
4 5 2 9 10 Returns: 30
Here are the edges of the tree you should generate: 0-1 (length 5), 0-3 (length 4), and 1-2 (length 6). For query 0 we return 0 because there are no blue nodes yet. Queries 1 and 2 instruct us to color vertices 0 and 3 blue. Then, query 3 asks us to compute the sum of distances between the vertex 2 and each of the blue nodes. The distance between 2 and 0 is 11, and the distance between 2 and 3 is 15. Hence the sum of all distances is 11+15 = 26. Similarly we can compute that the answer to the last query is 4+0 = 4.
8 8 3 5 7 Returns: 6
14750 50 29750 1157 21610 Returns: 2537640
21757 2156 27143 12487 21037 Returns: 100922593
Submissions are judged against all 152 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TreeColoring with a public method long long color(int N, int Q, int startSeed, int threshold, int maxDist) · 152 test cases · 2 s / 256 MB per case