luogu#P7929. [COCI 2021/2022 #1] Logičari

    ID: 7256 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2021树形 DP基环树COCI(克罗地亚)

[COCI 2021/2022 #1] Logičari

Problem Description

Given a unicyclic graph with nn vertices, you now need to color some vertices so that every vertex has exactly one adjacent vertex (excluding itself) that is colored. Find the minimum number of colored vertices, or report that there is no solution.

Input Format

The first line contains an integer nn.

The next nn lines each contain two integers ui,viu_i, v_i, meaning there is an edge between uiu_i and viv_i.

Output Format

Output the minimum number of colored vertices, or report that there is no solution. If there is no solution, output -1.

4
1 2
2 3
3 4
4 1
2
3
1 2
2 3
3 1
-1
7
1 2
2 3
3 4
4 5
5 6
6 7
2 4
4

Hint

Sample Explanation

Sample 1 Explanation

You can color vertices 11 and 22.

Sample 2 Explanation

If there is one colored vertex, then the colored vertex will have no colored adjacent vertex.

If there are two colored vertices, then an uncolored vertex will have two colored adjacent vertices.

Constraints

For all testdata, 3n1053 \le n \le 10^5, 1ui,vin1 \le u_i, v_i \le n.

Subtask Special Constraints Score
11 The degree of every vertex is 22 1010
22 n20n \le 20
33 n103n \le 10^3 4040
44 No special constraints 5050

Notes

The total score for this problem is 110110 points.

This problem is translated from T3 Logičari of Croatian Open Competition in Informatics 2021/2022 Contest #1.

Translated by ChatGPT 5