Rings and Strings
Time limit10sMemory limit512 MB
Given N rings and the black strings between them, choose a subset of rings to hang so that the number of black strings between hung and unhung rings is maximized.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Graph, Tree
- Solved
- No attempts yet
Problem
Misawa, an artist, is trying to make a new work. Misawa prepared a large number of metal rings and black and white strings of two colors.
Misawa can make a work by repeatedly tying two different rings together with a string of either color. A ring may be tied to several rings, but each pair of rings is joined by at most one string.
Misawa wants to make a good work, since it takes effort to make one. A 'good work' is defined as follows.
- A single ring is a good work.
- Given two good works, connecting every pair of rings that are not joined by a string in either work with a white string gives one good work. (Figure 1. Operation 1)
- Swapping every white string with a black string and every black string with a white string in a good work gives a good work. (Figure 1. Operation 2)
- Only the objects defined above are good works.
Figure 1 is an example of a good work.

Figure 1. Example of a good work
Misawa finished a good work that satisfies these conditions. Misawa will select zero or more rings in this work, hang them from the ceiling, and display them. Misawa wants the black strings to stand out as much as possible. In other words, Misawa wants to maximize the number of black strings connecting a ring hanging from the ceiling with a ring that is not hanging.
Find the maximum number of black strings connecting a ring hanging from the ceiling with a ring that is not hanging, when displayed this way.
Input
The input is given in the following format.
...
() is the number of rings in the work. () is the number of black strings in the work.
and () mean that the -th black string connects ring and ring . Any pair of rings not given in the input is connected by a white string.
The work given in the input is guaranteed to be a good work.
Output
Print the maximum number of black strings connecting a ring hanging from the ceiling with a ring that is not hanging.