CHAIN - Strange Food Chain

题意翻译

动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形。A 吃 B,B吃 C,C 吃 A。 现有 N 个动物,以 1 - N 编号。每个动物都是 A,B,C 中的一种,但是我们并不知道 它到底是哪一种。 有人用两种说法对这 N 个动物所构成的食物链关系进行描述: 第一种说法是“1 X Y”,表示 X 和 Y 是同类。 第二种说法是“2 X Y”,表示 X 吃 Y 。 此人对 N 个动物,用上述两种说法,一句接一句地说出 K 句话,这 K 句话有的是真 的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。 • 当前的话与前面的某些真的话冲突,就是假话 • 当前的话中 X 或 Y 比 N 大,就是假话 • 当前的话表示 X 吃 X,就是假话 你的任务是根据给定的 N 和 K 句话,输出假话的总数。 这题和[2024](https://www.luogu.com.cn/problem/P2024)不同的是,有T组数据。

题目描述

There are 3 kinds of animals A,B and C. A can eat B,B can eat C,C can eat A. It's interesting,isn't it? Now we have n animals,numbered from 1 to n. Each of them is one of the 3 kinds of animals:A,B,C. Today Mary tells us k pieces of information about these n animals. Each piece has one of the two forms below: - 1 x y: It tells us the kind of x and y are the same. - 2 x y: It tells us x can eat y.

输入输出格式

输入格式


The first line contains a single integer t.t blocks follow. To every block,the first line contains two integers n(1<=n<=50000) and k (1<=k<=100000). k lines follow,each contains 3 positive integers D(1<=D<=2),X,Y,separated by single spaces.

输出格式


t lines,each contains a single integer - the number of false pieces in the corresponding block.

输入输出样例

输入样例 #1

1
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5

输出样例 #1

3

Hint:
The false pieces are the 1st,the 4th and the 5th ones.