#P15787. [JAG 2025 Summer Camp #3] Flight Planning 2

[JAG 2025 Summer Camp #3] Flight Planning 2

题目描述

There are NN airports in a certain country, and the Jag Airlines Group operates MM flights connecting these airports. To reduce the number of airplanes required, they choose to rearrange the order of flights.

In each flight, an airplane departs from one airport and arrives at another, and each flight must be operated by exactly one airplane. We denote by aba \rightarrow b a flight that departs from airport aa and arrives at airport bb.

An airplane can operate a flight xyx \rightarrow y only if one of the following holds:

  • The airplane was initially placed at airport xx and has not yet operated any flight.
  • The destination of its most recent operation was airport xx.

For example, suppose there are three flights: aba \rightarrow b, cac \rightarrow a, and bdb \rightarrow d. If they are ordered as cac \rightarrow a, bdb \rightarrow d, and aba \rightarrow b, then cac \rightarrow a and aba \rightarrow b can be operated by the same airplane, so the three flights can be operated by two airplanes.

You may reorder the MM flights arbitrarily and choose the initial placements of the airplanes arbitrarily. What is the minimum number of airplanes required to operate all the flights?

输入格式

The input consists of multiple test cases.

The first line contains an integer TT (1T10001 \leq T \leq 1000), representing the number of test cases.

TT test cases follow. Each test case is given in the following format.

$$\begin{aligned} & N \ M \\ & a_{1} \ b_{1} \\ & \vdots \\ & a_{M} \ b_{M} \end{aligned}$$

For each test case, the first line contains integers NN (2N3000002 \leq N \leq 300\,000) and MM (1M3000001 \leq M \leq 300\,000), representing the number of airports and the number of flights, respectively.

The following MM lines each contain integers aia_i and bib_i, satisfying 1ai,biN1 \leq a_i, b_i \leq N and aibia_i \neq b_i. Each line describes the ii-th flight aibia_i \rightarrow b_i.

Additionally, the sum of NN over all test cases does not exceed 300000300\,000, and the sum of MM over all test cases does not exceed 300000300\,000.

输出格式

For each of the TT test cases, output the minimum number of airplanes required, one per line.

2
5 3
2 3
5 4
1 2
5 6
2 3
3 5
5 1
4 2
2 3
1 4
2
1