#P2273. [HNOI2002] 交换

    ID: 1242 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2002各省省选平衡树湖南概率论,统计

[HNOI2002] 交换

Description

Given nn integer registers r1,r2,,rnr_1, r_2, \cdots, r_n. We define a compare-exchange instruction CE(a,b)\text{CE}(a,b) as follows: if the value in rar_a is greater than the value in rbr_b, then swap the values in registers rar_a and rbr_b. Here, 1a<bn1 \leq a < b \leq n.

A compare-exchange program (abbreviated as a CE program) is a finite sequence of compare-exchange instructions. A CE program is called a MIN program if, after executing the program, the value in register r1r_1 is the minimum among all registers. If a CE program remains a MIN program after removing any single instruction from it, then the CE program is called a reliable MIN program.

Your task is: given a CE program PP, compute the minimum number of instructions that must be appended to the end of PP to make PP a reliable MIN program.

For example, consider the following CE program on three registers:

CE(1,2)\text{CE}(1, 2), CE(2,3)\text{CE}(2, 3), CE(1,2)\text{CE}(1, 2).

We only need to append two instructions: CE(1,3)\text{CE}(1, 3), CE(1,2)\text{CE}(1, 2) to make this CE program a reliable MIN program.

Input Format

There are two lines. The first line contains two space-separated integers nn and mm; here nn is the number of registers (2n1042 \leq n \leq 10 ^ 4), and mm is the number of instructions in the CE program (0m200000 \leq m \leq 20000).

The second line contains mm instructions CE(a,b)\text{CE}(a, b). Inside each instruction, aa and bb are separated by a comma, and different instructions are also separated by commas.

Output Format

Output a single integer on one line: the minimal number of instructions that should be appended.

3 3
1,2,2,3,1,2

2

Hint

Translated by ChatGPT 5