Source: http://poj.org/problem? Id = 1182
Food Chain
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:45355 |
|
Accepted:13229 |
Description
The animal kingdom contains three types of animals A, B, and C. The food chains of these three types constitute an interesting ring. A eats B, B eats C, and C eats.
There are n animals numbered 1-n. Every animal is one of A, B, and C, but we don't know which one it is.
There are two ways to describe the relationship between the food chains of the N animals:
The first statement is "1 x Y", indicating that X and Y are similar.
The second statement is "2 x Y", which indicates that X eats y.
This person speaks K sentences one by one for N animals in the preceding two statements. These K sentences are true or false. When one sentence meets the following three conditions, this sentence is a lie, otherwise it is the truth.
1) The current statement conflicts with some of the preceding actual statements;
2) In the current statement, X or Y is greater than N, which is false;
3) The current statement indicates that X eats X, which is a lie.
Your task outputs the total number of false statements based on the given n (1 <= n <= 50,000) and K statements (0 <= k <= 100,000.
Input
The first line is two integers N and K, separated by a space.
Each row in the following K rows contains three positive integers, D, X, and Y, which are separated by a space. D indicates the type of the statement.
If D = 1, X and Y are of the same type.
If D = 2, X eats y.
Output
Only one integer indicates the number of false statements.
Sample Input
100 71 101 1 2 1 22 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample output
3
Source
Noi 01
Question: vector and query set ~~ Do some common operations and check the set before doing this ~~ It took a while to understand the meaning of the word "vector. In this question, we assume three relationships: 0 represents the same type as the root node, 1 represents the root node, and 2 represents the root node. The difficulty lies in understanding vector offset ~~~ I have read a lot of questions before, basically speaking about finding rules and not telling the essence of things. To put it bluntly, vector offset is actually to use these relationships as vectors for computation, and directly calculate them directly than observe the law. Example: 1. then, the relationship between rootx and rootty is rootx --> X + X --> Y + y --> rooty ~~ 2. The root nodes of X and Y are both root nodes and the relationship between x and y; X --> root + root --> Y
This makes code implementation simple ~
AC code:
# Include <cstdio> const int max = 50001; int N, K, D, X, Y, Father [Max], R [Max], ANS = 0; int find (INT t) {If (t = Father [T]) return t; int temp = find (father [T]); R [T] = (R [T] + R [Father [T]) % 3; father [T] = temp; return temp;} int main () {scanf ("% d", & N, & K); For (INT I = 1; I <= N; I ++) {FATHER [I] = I; R [I] = 0;} while (k --) {scanf ("% d", & D, & X, & Y); If (x> N | Y> N | D = 2 & X = y) {ans ++; continue; // condition 2 3} int tempx = find (x), Tempy = find (y); If (tempx = Tempy) {If (R [x]-R [y] + 3) % 3! = D-1) ans ++;} else {FATHER [tempx] = Tempy; R [tempx] = (D + 2-r [x] + R [y]) % 3 ;}} printf ("% d \ n", ANS); Return 0 ;}
Poj 1182 food chain