Maximize the Intersections solution codeforces
On a circle liedistinct points, with the following property: however you choose chords that connect disjoint pairs of points, no point strictly inside the circle belongs to all chords. The points are numbered in clockwise order.
Initially,chords connect pairs of points, in such a way that all the endpoints of these chords are distinct.
You want to drawadditional chords that connect the remaining points (each point must be an endpoint of exactly one chord).
In the end, letbe the total number of intersections among all chords. Compute the maximum value that can attain if you choose the chords optimally.
Note that the exact position of thepoints is not relevant, as long as the property stated in the first paragraph holds.
The first line contains a single integer( ) — the number of test cases. Then test cases follow.
The first line of each test case contains two integersand ( , ) — half the number of points and the number of chords initially drawn.
Thenlines follow. The -th of them contains two integers and ( , ) — the endpoints of the -th chord. It is guaranteed that the numbers are all distinct.
For each test case, output the maximum number of intersections that can be obtained by drawingadditional chords.
4 4 2 8 2 1 5 1 1 2 1 2 0 10 6 14 6 2 20 9 10 13 18 15 12 11 7
4 0 1 14
In the first test case, there are three ways to draw the additional chords, shown below (black chords are the ones initially drawn, while red chords are the new ones):
In the second test case, there are no more chords to draw. Of course, with only one chord present there are no intersections.
In the third test case, we can make at most one intersection by drawing chords and , as shown below: