#P15071. [UOI 2024 II Stage] Matches

    ID: 17000 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心并查集2024二分图UOI(乌克兰)

[UOI 2024 II Stage] Matches

题目描述

Anton has invented a new team sport called waterball (similar to paintball, but with water). He wants to share his invention with his nn friends. Anton has good relationships with all his friends, but it's not guaranteed that his friends have the same relationships with each other. Specifically, we know that friend number aia_i conflicts with friend bib_i.

Anton was given a list of mm conflicting pairs (ai;bia_i;b_i). Now, it seems like it would be possible to divide the players into two teams, but it's not that simple for Anton...

He wants to divide these mm conflicting pairs into segments in such a way that:

  • each conflicting pair belongs to exactly one segment;
  • considering only the relationships within each segment, it should be possible to divide all the people into two teams in such a way that there are no two conflicting people in the same team.

For example, let's say we have an array of conflicting pairs [(1,2),(2,3),(1,3)][(1, 2), (2, 3), (1, 3)]. We can take the first two pairs in the first segment. In this case, we can form the teams: [1,3][1, 3] and [2][2]. In the second segment, we can take the last pair. 11 and 33 must be in different teams, and 22 can be in either team. Alternatively, we can assign the first pair to the first segment, and the last two to the second segment. Note that we cannot assign the first and third pairs to the same segment, and the second pair to another. This is because a segment should only contain consecutive pairs. We also cannot assign all pairs to the same segment, as there would always be a team where people conflict.

Anton has made the statement complicated again, and now he can't solve the problem. Help him by telling him the minimum number of segments into which he can divide the pairs to satisfy the above conditions.

输入格式

The first line contains two integers nn, mm (1n,m1061 \le n, m \le 10^6) --- the number of friends and the number of conflicting relationships among friends.

The next mm lines contain two integers each, aia_i, bib_i (1ai,bin1 \le a_i, b_i \le n, aibia_i \neq b_i), indicating that friend aia_i conflicts with friend bib_i.

It is guaranteed that no pair (a;ba;b) is repeated more than once.

输出格式

Print a single integer --- the answer to the problem.

3 3
1 2
2 3
1 3
2
5 10
2 4
1 2
3 4
1 3
1 5
4 5
2 3
3 5
1 4
2 5
3

提示

The first example is explained in the legend above.

In the second example, for instance, it is possible to divide into the following segments: [1;6][1;6], [7;9][7;9], [10;10][10;10].

In the first segment, the teams can be formed as [1,4][1,4], [2,3,5][2,3,5] --- 11 and 44 do not conflict with each other, as well as the pairs (2;3)(2;3), (2;5)(2;5), (3;5)(3;5).

In the second segment, the teams can be formed as [1,3][1,3], [2,4,5][2,4,5] --- 11 and 33 do not conflict with each other, as well as the pairs (2;4)(2;4), (2;5)(2;5), (4;5)(4;5).

In the third segment, the teams can be formed as [1,2][1,2], [3,4,5][3,4,5] --- 11 and 22 do not conflict with each other, as well as the pairs (3;4)(3;4), (3;5)(3;5), (4;5)(4;5).

Scoring

  • (44 points): n3n \le 3;
  • (77 points): n10n \le 10;
  • (1515 points): n,m5000n, m \le 5000;
  • (1313 points): conflicting pairs in the input are randomly generated; this means that mm pairs were randomly selected from all n(n1)2\frac{n (n-1)}{2} pairs;
  • (1414 points): each person conflicts with no more than 1010 people;
  • (1919 points): n105n \le 10^5;
  • (1717 points): n2105n \le 2 \cdot 10^5;
  • (1111 points): without additional restrictions.