#P16348. 「MierOI R1」Eternal (Hard ver.)
「MierOI R1」Eternal (Hard ver.)
Background
This problem is a harder version of P16280. The only difference between the two versions is that in this problem, .
Problem Description
Given closed intervals . Find the maximum number of intervals that can be selected such that for any two selected intervals that intersect, they share a common endpoint.
Two closed intervals and are said to intersect if and only if and .
Two closed intervals and are said to share a common endpoint if and only if , , , or .
Input Format
This problem has multiple test cases.
The first line contains a positive integer , representing the number of test cases.
Then follow test cases. For each test case:
- The first line contains a positive integer .
- The next lines each contain two positive integers .
Output Format
For each test case, output one line containing one integer, representing the maximum number of intervals that can be selected.
4
3
1 3
2 2
1 1
5
1 3
2 3
4 5
3 5
1 4
8
1 4
2 4
3 4
1 2
2 3
1 3
3 5
4 5
16
1 4
2 4
3 4
1 2
2 3
1 3
3 5
4 5
5 8
6 8
7 8
5 6
6 7
5 7
7 9
8 9
2
4
6
12
Hint
"Sample #1 Explanation"
For the first test case, you can select two intervals, and . It can be proven that there is no way to select more intervals.
For the second test case, you can select four intervals, , , , and . It can be proven that there is no way to select more intervals.
Constraints
This problem has no subtasks.
For all testdata, it is guaranteed that , , .
Translated by ChatGPT 5