LA 3523

LA 3523 - Knights of the Round Table

 * Problem Link

Summary
Given are N knights. Sometimes 2K+1 of them sit around a round table in such a way that no two neighbors hate each other. (Hate is symmetrical, if A hates B, then B hates A.) Output the number of knights that can never be a part of a such arrangement.

Explanation
The first step is to understand the problem statement correctly.

For example, consider 5 knights (A, B, C, D, E), where A hates D and E, B hates E, and C hates D. The only valid seating arrangement (up to symmetry) is A,B,C. Neither D nor E can ever sit at the round table, thus the answer is 2.

The solution consists in taking the graph "which pairs may be neighbors", splitting it into 2-connected components and checking whether each component is bipartite.

Proof: We want to find the set of vertices that lie on some odd cycle. Each odd cycle is completely contained in one of the 2-connected components. A graph contains an odd cycle iff it is not bipartite. It can be easily shown that IF (a graph is 2-connected) and (it contains an odd cycle) THEN each of its vertices lies on some odd cycle.

Gotchas
A bridge is a special type of a 2-connected component, you probably want to handle it separately.

Input
5 5 1 4 1 5 2 5 3 4 4 5 0 0

Output
2