Pinely Round 1 (Div. 1 + Div. 2)
E
Make It Connected
Codeforces
2400
原题
← DF1 →
翻译中…

You are given a simple undirected graph consisting of $n$ vertices. The graph doesn't contain self-loops, there is at most one edge between each pair of vertices. Your task is simple: make the graph connected.

翻译中…

You can do the following operation any number of times (possibly zero):

翻译中…
  • Choose a vertex $u$ arbitrarily.
  • For each vertex $v$ satisfying $v\ne u$ in the graph individually, if $v$ is adjacent to $u$, remove the edge between $u$ and $v$, otherwise add an edge between $u$ and $v$.
翻译中…

Find the minimum number of operations required to make the graph connected. Also, find any sequence of operations with the minimum length that makes the graph connected.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains a single integer $t$ ($1\leq t\leq 800$) — the number of test cases. The description of test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($2\leq n\leq 4000$) — the number of vertices in the graph.

翻译中…

Then $n$ lines follow. The $i$-th row contains a binary string $s_i$ of length $n$, where $s_{i,j}$ is '1' if there is an edge between vertex $i$ and $j$ initially, otherwise $s_{i,j}$ is '0'.

翻译中…

It is guaranteed that $s_{i,i}$ is always '0' and $s_{i,j}=s_{j,i}$ for $1\leq i,j\leq n$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $4000$.

翻译中…

Output

翻译中…

For each test case, in the first line, output an integer $m$  — the minimum number of operations required.

翻译中…

If $m$ is greater than zero, then print an extra line consisting of $m$ integers — the vertices chosen in the operations in your solution. If there are multiple solutions with the minimum number of operations, print any.

翻译中…

Note

翻译中…

In the first test case, the graph is connected at the beginning, so the answer is $0$.

翻译中…

In the second test case, if we do the operation with vertex $1$, we will get the following graph represented by an adjacency matrix:

翻译中…

$$ \begin{bmatrix} 0&1&1\\ 1&0&1\\ 1&1&0 \end{bmatrix} $$

翻译中…

It's obvious that the graph above is connected.

翻译中…

In the third test case, if we do the operation with vertex $3$ and $4$, we will get the following graph represented by an adjacency matrix:

翻译中…

$$ \begin{bmatrix} 0&1&1&1\\ 1&0&1&1\\ 1&1&0&1\\ 1&1&1&0 \end{bmatrix} $$

翻译中…

It's obvious that the graph above is connected, and it can be proven that we can't perform less than $2$ operations to make the graph connected.

翻译中…

样例

Input #1
4
3
011
100
100
3
000
001
010
4
0100
1000
0001
0010
6
001100
000011
100100
101000
010001
010010
Output #1
0
1
1
2
3 4
3
2 5 6
InputCopy OutputCopy
题目信息
题号
1761E
时间限制
1 s
内存限制
512 MB
标签
二分
暴力
构造
并查集
图论
贪心
矩阵
树
双指针

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