Munnabhai MBBS

3.9

24 votes
Problem

Munnabhai desires to become a very famous doctor. So he just enrolled in a famous university.
University offers N different courses. Munnabhai being very passionate towards his studies, wants to study maximum no. of subjects possible. But his college has a weird dependency systems. If he wants to study a course Y, he must need to complete some other course(s) X. Due to the faulty dependency system he is not able to study some of the courses.

Assume he studies only one subject at a time. Each of N courses have a unique code from 1 to N. Given the dependency system of the institute, find the number of courses he could study.

INPUT :
First line contains T - No. of test cases.
For each test case, first line contains two space separated integers N and M , no. of courses and no. of dependencies respectively.
Next M line contains space separated integers X and Y , where course X is prerequisite of course Y.

OUTPUT :
Print maximum possible subjects that Munnabhai can study.

CONSTRAINTS :
1 ≤ T ≤ 103
1 ≤ N ≤ 103
1 ≤ X , Y ≤ N
0 ≤ M ≤ 2*N
X ≠ Y

Problem Setter : Rajan Kasodariya

Sample Input
1
5 6
1 2
1 3
2 4
3 4
4 5
5 4
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

He can study 1 , 2 and 3 courses. but as 4 and 5 are dependent on each other he is not able to study that subjects. So ans is 3

Editor Image

?