Codeforces Round 1040 (Div. 1)
A
Double Perspective
Codeforces
1300
原题
←B →
翻译中…

For a set of pairs $S = \{(a_1, b_1), (a_2, b_2), \ldots, (a_m, b_m)\}$, where $a_i \lt b_i$ for all $1 \le i \le m$, we define $f(S)$ and $g(S)$ as follows:

翻译中…
  • Treating each $(a_i, b_i)$ as a segment on the number line, $f(S)$ is the length of their union. Formally, $f(S)$ is the number of integers $x$ such that there exists an $i$ ($1 \leq i \leq m$) where $[x, x+1] \subseteq [a_i, b_i]$.
  • Treating each $(a_i, b_i)$ as an undirected edge in a graph, $g(S)$ is the number of nodes that lie on at least one simple cycle with at least $3$ edges. Formally, $g(S)$ is the number of nodes $x_1$ such that there exists a path $x_1 \to x_2 \to \ldots \to x_k \to x_1$ in the graph, where $k \geq 3$ and all $x_1, x_2, \ldots, x_k$ are distinct.
翻译中…

For example, $S = \{(1,2), (2,4), (1,4), (4,5),(6,7)\}$, we can get $f(S) = 5$ and $g(S) = 3$.

翻译中…

You are given $n$ distinct pairs. Your task is to select a subset $S'$ of these pairs such that $f(S') - g(S')$ is maximized. You need to output the indices of the selected pairs.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains an integer $n$ ($1 \le n \le 3 \cdot 10^3$).

翻译中…

Next $n$ lines each contain two integers $a_i$ and $b_i$ ($1 \le a_i \lt b_i \le 2n$), representing a pair.

翻译中…

It is guaranteed that all pairs are distinct within the same test case.

翻译中…

It is guaranteed that the sum of $n^2$ over all test cases will not exceed $9 \cdot 10^6$.

翻译中…

Output

翻译中…

For each test case, the first line contains an integer $k$ ($0 \le k \le n$) — the size of the subset $S'$.

翻译中…

Next line contains $k$ integers $i_1, i_2, \ldots, i_m$ ($1 \le i_1 , i_2 , \ldots , i_k \le n$) — the indices of the selected pairs. Note the indices must be distinct.

翻译中…

Note

翻译中…

In the first test case, if no pair is selected (i.e. $S'=\varnothing$), $f(S')-g(S')=0-0=0$. If the first pair is selected, $f (S') - g (S')=1-0=1$. So the optimal solution is to select the first pair.

翻译中…

样例

Input #1
2
1
1 2
4
1 2
2 3
1 3
3 5
Output #1
1
1
3
1 2 4
InputCopy OutputCopy
题目信息
题号
2129A
时间限制
2 s
内存限制
256 MB
标签
构造
动态规划
并查集
图论
贪心
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。