#P3022. Odd Degree Subgraph
Odd Degree Subgraph
Odd Degree Subgraph
In the Cow Republic, there are N towns and M undirected paths between them. The invaders are coming, and the cows want to hinder them by shutting down some of the paths. Your task is to choose a subset of the paths to keep open so that every town is connected to an odd number of open paths. If no such subset exists, output NO
.
Note that the towns are numbered from 1 to N and the paths are given as pairs Ai and Bi (with 1 ≤ Ai, Bi ≤ N, Ai ≠ Bi). There are no duplicate paths. The republic might be disconnected.
A key observation is that since every kept path contributes 1 to the degree of two towns, the sum of all degrees in the kept subgraph equals 2×(number of kept paths) and must be even. Hence a necessary (and in fact sufficient) condition is that each connected component must contain an even number of towns.
If a solution exists, you should print YES
followed by the number of kept paths K and then the indices (1-indexed, referring to the input order) of the kept paths (in any order) that yield the required odd degrees at every town. Otherwise, print NO
.
It can be shown that a solution (if one exists) can be obtained by finding a spanning forest and then choosing a certain subset of the tree edges. One way to do this is to assign each town a deficit of 1 (meaning it needs an odd number of incident edges), and then perform a DFS in each connected component. For each tree edge from parent to child, if the child’s deficit is 1 then select that edge and flip the deficit of the parent. This yields a valid set of kept paths.
All formulas in this statement are rendered in ( \LaTeX ) (for example, the requirement for each town is ( \text{deg}(v) \equiv 1 \pmod{2} )).
inputFormat
The first line contains two integers N and M (1 ≤ N ≤ 50,000, 1 ≤ M ≤ 100,000).
Each of the following M lines contains two integers Ai and Bi describing an undirected path.
outputFormat
If a subset exists such that every town has an odd number of incident paths, print YES
on the first line.
On the next line, print an integer K denoting the number of kept paths, and on the following line print K space‐separated integers corresponding to the indices (1-indexed) of the kept paths in any order.
If no such subset exists, output NO
.
sample
4 4
1 2
1 3
2 3
3 4
YES
2
1 4
</p>