Unreachable Nodes in a DAGMedium

Unreachable Nodes in a DAG

MediumPractice library · +20 XPgraphstopological sortbfs

You are given a directed acyclic graph (DAG) with $N$ nodes and $M$ edges. Some nodes might have no incoming edges. We define 'source nodes' as nodes with an in-degree of 0. All nodes reachable from any source node are considered 'reachable'. Your task is to count the number of nodes that are not reachable from any source node.

Input

The first line contains two integers $N$ and $M$, the number of nodes and edges. The following $M$ lines each contain two integers $u$ and $v$, representing a directed edge from node $u$ to node $v$. Nodes are numbered from 1 to $N$.

Output

Print a single integer: the count of unreachable nodes.

Constraints

1 \le N \le 200000, 0 \le M \le 200000. The graph is guaranteed to be a Directed Acyclic Graph (DAG).

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5 3
1 2
1 3
2 4
Output
0

Nodes 1, 2, 3, 4 are reachable from source node 1. Node 5 is unreachable.

Example 2

Input
4 4
1 2
1 3
2 4
3 4
Output
0

Nodes 1, 2, 3, 4 are all reachable from source node 1.

Hints