#P4538. [AHOI2006] 棋盘上的问题

[AHOI2006] 棋盘上的问题

Description

Keke and Kaka drew a huge NNN*N chessboard. They want to place as many international chess rooks as possible on this board so that none of them can attack each other (a rook can attack along rows and columns).

But the answer is obviously the board width NN, so Keke restricts that pieces can only be placed on a finite set of MM positions on the board. (Other positions cannot hold pieces, but rooks can still pass through them to attack other pieces.) However, this does not stump the two smart kids; they quickly figure out that the answer is KK.

Then Kaka raises another question: If we remove one position from these MM allowed positions while still ensuring that at most KK rooks can be placed (i.e., the maximum remains KK), how many feasible choices are there?

[Task] Write a program to:

  • Read from the input file the size of the board and the positions on the board where pieces can be placed.
  • Compute the number of feasible choices as Kaka described.
  • Output your answer.

Input Format

The first line contains two positive integers NN and MM, representing the size of the board and the number of positions where pieces can be placed.

The following MM lines each describe one position with two integers xix_i and yiy_i, indicating that this position is at row xix_i, column yiy_i of the board.

The same position will not be described twice.

Output Format

The output contains a single integer, representing the number of feasible choices.

3 4
1 2
1 3
2 2
2 1
4

Hint

For all testdata, it is guaranteed that: $1\le i \le M\le 6\times 10^5,1\le x_i,y_i\le N\le 2\times 10^5$.

Translated by ChatGPT 5