Food Chain
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:43526 |
|
Accepted:12679 |
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
Okay .. This is indeed the most detailed .. Click Open Link
After reading it, I learned more about the query set...
Thank you for your patience ..
In addition, I used multiple inputs to solve this problem ..
It took me a long time ..
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <queue> # include <sstream> # include <cmath> using namespace STD; # define M 100500int N; int K; int D; int X; int y; int P [m]; int R [m]; int ans; void start () {for (INT I = 1; I <= N; I ++) {P [I] = I; // initialize and query the set R [I] = 0; // during initialization, each element acts as a set and its element is 1} int find (int x) // query the find {If (P [x] = x) return X; else {int T = P [X]; P [x] = find (P [x]); R [x] = (R [x] + R [T]) % 3; return P [x] ;}} void Kruskal (int x, int y, int d) {int xx = find (x); int YY = find (y); P [YY] = xx; R [YY] = (R [x]-R [y] + 3 + (D-1) % 3;} int main () {scanf ("% d ", & N, & K); Start (); ans = 0; For (INT I = 1; I <= K; I ++) {scanf ("% d", & D, & X, & Y ); if (x> N | Y> N | (D = 2 & X = y) ans ++; else if (find (X) = find (y) {If (D = 1 & R [x]! = R [y]) ans ++; If (D = 2 & (R [x] + 1) % 3! = R [y]) ans ++;} else Kruskal (X, Y, d);} printf ("% d \ n", ANS); Return 0 ;}