6988.Flight Route Requests

Time Limit: 1s Memory Limit: 512MB

There are $n$ cities with airports but no flight connections. You are given $m$ requests which routes should be possible to travel.

Your task is to determine the minimum number of one-way flight connections which makes it possible to fulfil all requests.

Input Format(From the terminal/stdin)

The first input line has two integers $n$ and $m$ : the number of cities and requests. The cities are numbered $1,2,\dots,n$ .

After this, there are $m$ lines describing the requests. Each line has two integers $a$ and $b$ : there has to be a route from city $a$ to city $b$ . Each request is unique.

  • $1 \le n \le 10^5$
  • $1 \le m \le 2 \cdot 10^5$
  • $1 \le a, b \le n$

Output Format(To the terminal/stdout)

Print one integer: the minimum number of flight connections.

Sample Input

Copy
4 5
1 2
2 3
2 4
3 1
3 4
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output

Copy
4
 \n

You can create the connections $1 \rightarrow 2$ , $2 \rightarrow 3$ , $2 \rightarrow 4$ and $3 \rightarrow 1$ . Then you can also fly from city $3$ to city $4$ using the route $3 \rightarrow 1 \rightarrow 2 \rightarrow 4$ .

Source: CSES, Advanced Graph Problems, 1699

Submit

请先 登录

© 2025 FAQs