CPU
Time limit2sMemory limit128 MB
Given chords listed by importance with each vertex in at most one chord, find the longest prefix that can be 2-colored so no color class self-crosses.
- Level
Medium6 of 10
- Topics
- Graph, Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
Aiden is designing a low-cost processor to be assembled on a circuit board from standard components. Components are joined by connecting wires. A wire may bend freely and need not be straight, but it must never cross a component or another wire.
The basic layout is already finished: all components are wired together into a single closed loop, the main loop, and they lie around this loop in consecutive order, numbered through . To speed the processor up, Aiden now wants to add some extra direct wires between pairs of components. Each component may receive at most one extra wire.
He has written down every extra connection he would like, ordered from most important to least important. He will keep the most important of them — the first entries of the list — with chosen as large as possible so that all kept wires can be drawn simultaneously without any two of them crossing.
Since the components lie on a loop, each extra wire can be routed either inside the loop or outside it. Two wires placed on the same side cross exactly when their endpoints alternate around the loop; wires on opposite sides never cross. Hence a set of connections can be drawn without crossings if and only if every connection can be assigned to the inside or the outside so that no two connections on the same side have interleaving endpoints.
Given the loop size and the importance-ordered list of desired connections, determine the largest possible value of .
Input
The first line contains an integer (), the number of components on the main loop.
The second line contains an integer (), the number of extra connections under consideration.
Each of the next lines contains two integers and (, ): a desired connection between components and . The connections are given in descending order of importance. No connection joins a component to itself, a component may be joined to a neighbouring component of the main loop, and no component appears in more than one connection.
Output
Print a single integer: the largest possible value of .