**Link:**

Http://poj.org/problem? Id = 1182

**Original question:**

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

**Analysis and Summary:**This is a slightly complex type and involves three types of animals. Obviously, it should be divided into three categories.

Assume that X eats y, So X is greater than Y. Because there are three animals in total, the weights are divided into 0, 1 and 2. 0 is the root node. If the weight of one node is 1, it indicates that the node is rooted. If the weight is 2, 2 indicates that 2 is eating 1 (2 is greater than 1), and because of the relationship between the three animals, 2 is eating 0.

Then, their weights are calculated based on the link classification during the merge.

**Code:**

# Include <cstdio> const int n = 50005; int N, M, F [N], rank [N]; inline void Init () {for (INT I = 0; I <= N; ++ I) f [I] = I, rank [I] = 0;} int find (INT X) {If (x = f [x]) return X; int fa = f [X]; F [x] = find (F [x]); rank [x] = (rank [x] + rank [fa]) % 3; return f [X];} inline bool Union (int x, int y, int D) {int A = find (x), B = find (y); if (a = B) {If (D = 1 & rank [x]! = Rank [y]) return false; If (D = 2) {If (rank [x] = 2 & rank [y]! = 1) return false; If (rank [x] = 1 & rank [y]! = 0) return false; If (rank [x] = 0 & rank [y]! = 2) return false;} return true;} f [a] = B; If (D = 2) {rank [a] = (rank [y]-rank [x] + 3 + 1) % 3; // Add 3, because it may be a negative number} else {rank [a] = (rank [y]-rank [x] + 3) % 3; // note that 3 is added, because it may be a negative number} return true;} int main () {int X, Y, D; scanf ("% d", & N, & M ); init (); int CNT = 0; For (INT I = 0; I <m; ++ I) {scanf ("% d", & D, & X, & Y); If (x> N | Y> N | D = 2 & X = y) {++ CNT; continue ;} if (! Union (X, Y, d) {++ CNT ;}} printf ("% d \ n", CNT); Return 0 ;}

*-- The meaning of life is to give it meaning.*

*Original*
*Http://blog.csdn.net/shuangde800*
*, By d_double (reprinted, please mark)*