CF1761E.Make It Connected

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a simple undirected graph consisting of nn 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 uu arbitrarily.
  • For each vertex vv satisfying vuv\ne u in the graph individually, if vv is adjacent to uu , remove the edge between uu and vv , otherwise add an edge between uu and vv .

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.

输入格式

Each test contains multiple test cases. The first line contains a single integer tt ( 1t8001\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 nn ( 2n40002\leq n\leq 4000 ) — the number of vertices in the graph.

Then nn lines follow. The ii -th row contains a binary string sis_i of length nn , where si,js_{i,j} is '1' if there is an edge between vertex ii and jj initially, otherwise si,js_{i,j} is '0'.

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

It is guaranteed that the sum of nn over all test cases does not exceed 40004000 .

输出格式

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

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

输入输出样例

  • 输入#1

    4
    3
    011
    100
    100
    3
    000
    001
    010
    4
    0100
    1000
    0001
    0010
    6
    001100
    000011
    100100
    101000
    010001
    010010

    输出#1

    0
    1
    1
    2
    3 4 
    3
    2 5 6

说明/提示

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

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

[011101110]\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 33 and 44 , we will get the following graph represented by an adjacency matrix:

[0111101111011110]\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 22 operations to make the graph connected.

首页