#P2002. 消息扩散

    ID: 946 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>图论洛谷原创强连通分量,缩点

消息扩散

Description

There are nn cities connected by one-way roads. Messages spread along the roads. Given nn cities and the roads between them, ask for the minimum number of cities where you need to initially publish the message so that all nn cities receive it.

Input Format

The first line contains two integers n,mn, m, meaning nn cities and mm directed roads. In the next mm lines, each line contains two integers b,eb, e, indicating there is a road from bb to ee. Multiple edges and self-loops are allowed.

Output Format

Output a single integer, the minimum number of cities where the message must be published.

5 4
1 2
2 1
2 3
5 1

2

Hint

  • Sample Explanation #1
    In the sample, publish the message in cities 44 and 55.

  • Constraints
    For 20%20\% of the testdata, n200n \le 200.
    For 40%40\% of the testdata, n2000n \le 2000.
    For 100%100\% of the testdata, 1n1051 \le n \le {10}^5, 1m5×1051 \le m \le 5 \times {10}^5.

Translated by ChatGPT 5