CHAIN - Strange Food Chain

no tags 

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.

Some of these k pieces are true, some are false. The piece is false if it satisfies one of the 3 conditions below, otherwise it's true.

  • X or Y in this piece is larger than n.
  • This piece tells us X can eat X.
  • This piece conflicts to some true piece before.

Input

The first line contains a single integer t. t blocks follow.

For 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.

Output

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

Example

Input:
1
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5

Output:
3

Hint

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

Warning: large Input/Output data, be careful with certain languages.

hide comments
prafulagg_459: 2024-10-08 14:32:11

anyone looking for the tutorial/a walkthorugh
this video can help: https://youtu.be/WpYSEbDw4lM?si=JTu0pqiUTkI3X2Zl

rohit_2112: 2024-07-29 06:55:10

where we can get the editorial of this problem my solution is almost correct but still getting wrong answer

piaoyang123: 2024-04-16 06:32:53

https://www.luogu.com.cn/problem/P2024

sky_0509: 2023-08-30 11:54:31

bhai yaar sawal to sahin se likha karo

johnny_always: 2021-01-03 03:28:06

I get Accepted in 0.06 in C++ but TLE in Python with the same solution... Is there any optimization that pass the time limit?

[NG]: 1) Experiment with I/O routines at INTEST until you get under ~1.30s, 2) Submit in PyPy, 3) Your C code is much slower than top solutions so look for generic redundancies and algo improvements also.

Last edit: 2021-01-03 03:54:40
paxton: 2020-09-22 20:42:03

@scolar_fuad
from previous inputs 1(let A) can eat 2(let B) , 2 can eat 3(let C) , so , if it is 3(C) can eat 1(A) which is 6th case then it will be true. I hope u get it : )

h111801015: 2020-08-17 08:51:28

Can anyone help me with the solution and explaination?

scolar_fuad: 2019-08-14 19:23:09

why 5th query is false ?

bdezso: 2019-06-30 11:28:09

nice problem!!: )

Last edit: 2019-06-30 13:03:49
daridius: 2019-01-14 15:26:54

this is cool :)

Last edit: 2019-01-14 19:11:18

Added by:Fudan University Problem Setters
Date:2007-03-31
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:Chinese National Olympiad in Informatics 2001,Day 1; translated by Blue Mary