Domino Effect

View as PDF

Submit solution

Points: 10
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

There are N dominoes arranged in a peculiar order labeled from 1 to N.

Some dominoes knock down other dominoes when they are knocked down.

It is possible to knock down an entire chain of dominoes by knocking down a single one.

You are given a list of M pairs in the form a b, where domino a will knock down domino b when knocked down.

Determine the minimum number of dominoes you need to knock down manually in order to knock down all of them.

Constraints

1 \le N, M \le 10^5

1 \le a, b \le N

a \ne b

Input Specification

The first line of input contains 2 space-separated integers N and M.

The next M lines of input each contain 2 space-separated integers a and b.

Output Specification

Output a single integer, the minimum number of dominoes you need to knock down manually.

Sample Input 1

4 3
2 3
3 4
4 1

Sample Output 1

1

Explanation for Sample 1

It is optimal to knock down domino 2, which will result in the following chain: 2 \rightarrow 3 \rightarrow 4 \rightarrow 1.

Sample Input 2

5 4
3 4
2 3
2 1
1 2

Sample Output 2

2

Explanation for Sample 2

Knocking down domino 2 will knock down dominoes 1, 3, and 4.

Domino 5 needs to be knocked down separately.


Comments


  • -9999
    348929050  commented on May 18, 2023, 12:39 p.m. edit 2

    This comment is hidden due to too much negative feedback. Click here to view it.