POJ 1182 food Chain
Time limit:1000ms Memory limit:10000k
"Description-Title description" |
There are three types of animal a,b,c in the animal kingdom, and the food chain of these three animals is an interesting ring. A eat B, B eat c,c eat a. Existing n animals, numbered with 1-n. Every animal is one of the a,b,c, but we don't know what it is. Some people describe the food chain relationship between these n animals in two ways: The first argument is "1 x y", which means that x and Y are homogeneous. The second argument is "2 x y", which means x eats y. This person to n animals, with the above two statements, a sentence after sentence to say K sentence, this k sentence some is true, some false. When one sentence satisfies one of the following three, the sentence is a lie, otherwise it is the truth. 1) The current words conflict with some of the preceding words, which is false; 2) The current word in x or y is greater than N, is a lie; 3) The current words say x eats x, is a lie. Your task is to output the total number of falsehoods according to the given N (1 <= n <= 50,000) and the K-sentence (0 <= K <= 100,000). |
"Input-enter" |
"Output-out" |
The first line is two integers n and K, separated by a single space. The following k lines each line is three positive integer d,x,y, and two numbers are separated by a space, where D denotes the kind of claim. If d=1, it means that x and Y are homogeneous. If d=2, it means x eats y. |
There is only one integer that represents the number of false lies. |
"Sample Input-Enter sample" |
"Sample output-Output sample" |
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5 |
3 |
Exercises
and check Set
There are only three species of animals, and for different kinds of animals it is not eating or being eaten.
The relationship between [natural predators] and [food] is affected by the changes in their relationships with other species each time.
It is therefore necessary to record the [self] [natural predators] [food] three kinds of relationships
(Baidu seems to have a way to calculate the relationship after a bit ...) Visual inspection is the compression of the relationship)
Because I see (ping) Shui (Zha), I recorded all three of these relationships.
----------Off-Topic
Before himself to die, except [oneself] initial and other all with 0/-1, results debug a week desperate, the final result is always close to point.
The original code to see a lot of times, the suspicion is that the judgment conditions and Merge mode will conflict, debug after the bug snowball ...
Sure enough, this slag level is still a big dead, and then almost abandoned the pit when I saw the direct open int xx[3n] record way, instant explosion, oh no, clairvoyant, then minute rewrite/changed their code a again
---------back to the chase.
When initializing [self] [predators] [food] It is best to have different values, so it is easy to write, otherwise ...
This time [self] [predators] [food] can be placed in an array, it is convenient to query and merge the root in the same number of groups.
For any one element a
[Self] position = a
[predators] Position = a + N
Location of [Food] = a + 2n
Take a B as an example
A [natural predators] [self] [food]
b [Natural predators] [self] [food]
A>n | | B>n can go straight to the fake.
For the first argument 1 a B
If a eats B or B eats A is false
Otherwise merge A and B's [self] [predators] [food]
For the second argument 2 a B
If a B is homologous or b eats a, it is false.
Otherwise merge a[predators] and b[food],a[itself] and b[food], a[food] and [predators].
(This feeling is a bit like the determinant of a matrix, so you can pull out the relationship of the method?) Lazy Cancer attack ... )
"Code C + +"
1#include <stdio.h>2 intdata[150005];3 intFidinta) {4 returnData[a] = = a? A:data[a] =FID (Data[a]);5 }6 BOOLIsfriend (intAintb) {7 returnFID (a) = =FID (b);8 }9 intMain () {Ten intN, K, I, J, A, B, C, opt; Onescanf"%d%d", &n, &k); A for(i = opt =0, N *=3; I < n; ++i) Data[i] =i; -N/=3; - while(k--){ thescanf"%d%d%d", &c, &a, &b); - if(A>n | | b >N) { -++opt; - Continue; + } - if(c = =1){ + if(Isfriend (A + N, b) | | isfriend (b + N, a)) + +opt; A Else for(i =0; I <3; ++i, A + = N, b + = n) data[fid (a)] =FID (b); at } - Else{ - if(Isfriend (A, b) | | isfriend (A + N, b) + +opt; - Else for(i =0, j =1; I <3; j = (i +2) %3, ++i) Data[fid (A + n*i)] = FID (b + Nj); - } - } inprintf"%d", opt); - return 0; to}
POJ 1182 food Chain