POJ 1182 food chain
http://acm.pku.edu.cn/JudgeOnline/problem?id=1182
The topic tells that there are 3 kinds of animals, eat with each other and be eaten, now tell you M sentence, in which there is really false, so that you judge the number of false (if there is no previous conflict with the current words, that is to say that the truth)
There are several ways of doing this, and my previous practice is that each set (or sub-tree, saying that the collection number is equivalent to the root node of the subtree, a concept) in the elements are divided into a, B, C three class, when merging changes the type of root node, other points corresponding to change the offset. However, this formula is difficult to push, especially the offset is easy to calculate errors.
Here's a general and easy-to-understand way to do this:
First, each point in the collection is recorded in relation to its relative relationship to the root node of this set (or sub-tree) relation. 0 means it is the same as the root node, 1 means it eats root nodes, and 2 means it is eaten by root nodes.
So judging the relationship between the two points A, B, we make P = Find (a), q = Find (b), i.e. p, q are the root nodes of a and B subtree, respectively.
1. if p! = q, stating that A, B is not related for the time being, then the judgment on them is correct, then merge the two subtrees. Here is the key, how to merge two subtrees so that the merged new tree can be guaranteed correct? Here we stipulate only p merge to Q (just said, the optimization effect of heuristic merging is not so obvious, if we use heuristic merger, we should introduce two formulas, and this push is a relatively tired of the work ... So generally we have a sub-tree set to another subtree). So after the merger, P's relation must change, so how much is it changed? The method here is to find a pattern, to list some of the possible circumstances, it is almost possible to introduce a formula. Here is the equation: tree[p].relation = (tree[b].relation–tree[a].relation + 2 + d)% 3; The d here is the relationship between A and B in the judgment statement. There is also a question, do we need to traverse the whole a subtree and update the state of each node? The answer is no, because we can modify it slightly in the Find () function, that is, node X inherits its father (note that the father will change after the path is compressed), i.e. it will inherit the change of p node, so we do not need to traverse past updates.
2. If p = q, it indicates a, B is already related. Then we can judge whether the statement is right, and also find the law to introduce the formula. That is if ((tree[b].relation + D + 2)% 3! = tree[a].relation), then this is the wrong word.
3. Make some changes to the find () function, that is, before the path is compressed before recording who the father is, and then after the path compression, update the state of the point (by inheriting the state of the former father, this time the state of the former father is updated).
The two functions of the core are:
int Find (int x)
{
int temp_p;
if (tree[x].parent! = x)
{
Because of the path compression, the node's relationship to the root node is updated (because it may not have been updated in the previous merge).
Temp_p = tree[x].parent;
Tree[x].parent = Find (tree[x].parent);
The relationship between x and the root node is updated (because the root node is changed), at which point the temp_p is the root node of its original subtree.
Tree[x].relation = (tree[x].relation + tree[temp_p].relation)% 3;
}
return tree[x].parent;
}
void Merge (int a, int b, int p, int q, int d)
{
The formula is introduced by law.
tree[p].parent = q; Here the subscript is the same, all Tree[p].
Tree[p].relation = (tree[b].relation–tree[a].relation + 2 + d)% 3;
}
And this kind of record and the root node relation method, applies to almost all and checks the set judgment relation (at least I have not encountered the situation which does not apply now ...) Maybe I did too little ...), so we strongly recommend ~ ~
Fix the food chain this problem, basic POJ on most of the basis and check set the topic can shun the second, here only the topic number: POJ 1308 1611 1703 1984 1986 (LCA Tarjan algorithm + and check set) 1988 2236 2492 2524.
Here are some of the topics that are slightly improved:
POJ 1456 Supermarket
http://acm.pku.edu.cn/JudgeOnline/problem?id=1456
The greedy idea of this problem is obvious, but the complexity of O (n^2) is obviously not, we can use the heap to optimize, here is the optimization method (very ingenious) to check the set. We consider the contiguous occupied interval as a set (subtree) whose root node is the first unoccupied interval to the left of the interval.
Sort first, and then each time the Find (B[i]) is greater than 0, greater than 0 indicates that there is unoccupied space on the left, then takes it, then merges (B[i], Find (B[i]) –1). Also here we specify only the left subtree to merge to the right of the subtree (think about why).
POJ 1733 Parity Game
http://acm.pku.edu.cn/JudgeOnline/problem?id=1733
The same problem is similar to the idea of a food chain.
First we discretization, because the original interval is too large (10^9), we can be separated according to the number of problems (10^4). We have to understand that the discretization here does not affect the final result, because the number of odd and even 1 in the interval is independent of the size of the interval (this is a bit strange, you can ignore ...), and then each time you enter a, B, we put b++, if they are within a set, then the interval [a, b] 1 of the number is equivalent to b.relation ^ a.relation, judge right and wrong can. If not within a collection, merge the collection (here we specify that the root node is small subtree merge root node large, so to push the formula according to different circumstances), modify the state of the root node of the subtree, the other node state of the subtree is updated by the Find () function.
HDU 3038 How many Answers is wrong
http://acm.hdu.edu.cn/showproblem.php?pid=3038
The above problem of the enhanced version, do not need to be discretized, because the interval and the size of the interval (and the above sentence, the same can be ignored ...), the practice is similar to the above question, but the formula has changed, the push on their own to be done. However, there is a condition, that is, the value of each point between [0, 100], then if a, B is not in a subtree, we will merge, but before merging to determine whether the combination will make the interval and illegal, if it would indicate that the merger is unlawful, then do not merge, it is also believed that the sentence is wrong.
POJ 1417 True Liars (difficult)
http://acm.pku.edu.cn/JudgeOnline/problem?id=1417
and check the Set + DP (or search).
The topic tells two kinds of people, a kind of only to tell the truth, a kind of only say lies. Then tell the M-statement, and ask if you can tell who is the only person who speaks the truth.
In fact, and check the part of the food chain is similar to, and the species has become less one, easier. We can combine some people with relationships into a set (see the food chain for details).
The problem now is that there are n sets, each with a, B with a number, and now requires that N sets jump out of a single number (a or b), making them equal to N1 (the number of people telling the truth). And this with DP can be a good solution, with F[i][j] to the first set and the number of J cases, we have also used Pre[i][j] record is currently selected A or B, for the later judgment State. The equation is f[i][j] = F[i–1][j–a] + f[i–1][j–b], J >= A, J >= B. If the last f[n][n1] = = 1 Description is the only case, output the case, otherwise output "no" (multi-Solution No)
Note the point:
1. This problem of M, N1, N2 are likely to appear 0, can be special treatment, can also be processed together.
2. Press the DP notation above, F[I][J] may be very large, because n can reach three digits. In fact, all we care about is f[i][j] equals 0, equals 1, more than 13 cases, so when f[i][j] > 1 o'clock, we all make it equal to 2.
POJ 2912 Rochambeau (difficult)
http://acm.pku.edu.cn/JudgeOnline/problem?id=2912
Baidu Star 2006 Preliminary the topic, feel very good, in and look up the topic is more difficult. In fact, this problem with the food chain is completely a Mo Zi, the same three kinds of food, the same restriction relationship. So there's no need to change the food chain code. But there is a judge, he can make arbitrary gestures. So what we do is, enumerate each child as judge, judge him as judge in the first few words of error err[i] (that is, to the first few words can judge that the child is not judge).
1. If only 1 children are judge when all statements are correct, stating that the child is judge, then the number of sentences judged is the maximum value of the other child's err[i]. If
2. If each child is not judge (that is, you can find the wrong statement), then it is impossible.
3. More than 1 children are judge when no error is found, that is, can not determine.
ZOJ 3261 Connections in Galaxy War
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563
Original URL: http://hi.baidu.com/czyuan_acm/home
* and check the title of the set *