CF2208D1.Tree Orientation (Easy Version)

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

This is the easy version of the problem. The difference between the versions is that in this version, the constraint on nn is lower. You can hack only if you solved all versions of this problem.

You once had an undirected tree with nn nodes. To make the tree look more interesting, you decided to assign an arbitary direction to each of the n−1n-1 edges.

As time goes by, you forgot the structure of your tree. However, you found a note which recorded after the direction of the edges have been assigned, whether uu can reach vv∗^{\text{∗}} for all ordered pairs of (u,v)(u,v) which satisfies 1≤u,v≤n1\le u,v\le n.

You want to find out the structure of the tree and the direction of the edges from the information given by the note. Determine if there is possible solution and construct one. If there are multiple solutions, you only need to find one of them.

∗^{\text{∗}}For a directed graph, we say that xx can reach yy if and only if there exists a sequence of nodes u1,u2,…,uku_1,u_2,\ldots,u_k such that u1=x,uk=yu_1=x,u_k=y and for all ii from 22 to kk, the directed edge ui−1→uiu_{i-1}\rightarrow u_i exists. In particular, a node can always reach itself.

这是该问题的简单版本。两个版本的区别在于,本版本中对 nn 的约束更小。仅当您解决了该问题的所有版本时,才可进行 Hack。

你曾经拥有一棵包含 nn 个节点的无向树。为了让这棵树看起来更有趣,你决定为它的 n−1n-1 条边任意指定一个方向。

随着时间推移,你忘记了这棵树的原始结构。然而,你发现了一张笔记,其中记录了在所有边被赋予方向之后,对所有满足 1≤u,v≤n1 \le u, v \le n 的有序对 (u,v)(u, v),节点 uu 是否能够到达节点 vv∗^{\text{∗}}。

你想根据这张笔记所给的信息,还原出原树的结构以及各边的方向。请判断是否存在可行解,并构造出一个可行解。若存在多个可行解,你只需给出其中一个即可。

∗^{\text{∗}} 对于有向图,我们称 xx 可以到达 yy,当且仅当存在一个节点序列 u1,u2,…,uku_1, u_2, \ldots, u_k,使得 u1=xu_1 = x、uk=yu_k = y,且对每个从 22 到 kk 的 ii,均存在有向边 ui−1→uiu_{i-1} \rightarrow u_i。特别地,每个节点总能到达其自身。

输入格式

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

The first line of each test cases contain an integer nn (2≤n≤5002\le n\le 500) denoting the number of nodes your tree have.

The following nn lines contain a string sis_i. sis_i is of length nn and consists only of 00 and 11. The jj-th character of sis_i is 11 if and only if ii can reach jj after the edges are directed.

It is guaranteed that the sum of n3n^3 over all test cases does not exceed 5003500^3.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤5002\le n\le 500),表示树中节点的数量。

接下来的 nn 行,每行包含一个字符串 sis_i。sis_i 的长度为 nn,且仅由字符 00 和 11 组成。sis_i 的第 jj 个字符为 11 当且仅当在将边定向后,节点 ii 可以到达节点 jj。

保证所有测试用例的 n3n^3 之和不超过 5003500^3。

输出格式

For each testcase, output Yes\texttt{Yes} if a solution exists, otherwise print No\texttt{No}. If the answer is Yes\texttt{Yes}, on the following lines output a description of the edges constructed.

Output n−1n-1 lines denoting the directed edges. Each line should contain two integers xx and yy, denoting that after the edges are directed, the directed edge x→yx\rightarrow y exists. If there are multiple solutions, print any of them.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

对于每个测试用例,若存在解,则输出 Yes\texttt{Yes};否则输出 No\texttt{No}。若答案为 Yes\texttt{Yes},则在接下来的行中输出所构造边的描述。

输出 n−1n-1 行,每行表示一条有向边。每行应包含两个整数 xx 和 yy,表示在对边进行定向后,存在有向边 x→yx\rightarrow y。若存在多种解,输出任意一种即可。

你可以以任意大小写形式输出答案(大写或小写均可)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答。

输入输出样例

  • 输入#1

    11
    4
    1000
    1111
    1010
    0001
    4
    1111
    0111
    0010
    0111
    4
    0011
    0111
    0011
    0001
    4
    1000
    0110
    0010
    1111
    4
    1000
    0110
    1010
    1111
    5
    10000
    01011
    00111
    00010
    00001
    5
    10000
    11000
    10101
    10111
    00001
    5
    10000
    01101
    00100
    01110
    10001
    4
    1100
    0100
    0011
    0001
    4
    1110
    0100
    0010
    0101
    3
    100
    111
    101

    输出#1

    Yes
    2 3
    2 4
    3 1
    No
    No
    Yes
    2 3
    4 1
    4 2
    No
    No
    Yes
    2 1
    3 1
    3 5
    4 3
    No
    No
    Yes
    1 2
    1 3
    4 2
    Yes
    2 3
    3 1

说明/提示

For the first test case, nodes 11 and 44 can only reach themselves, node 22 can reach every node, node 33 can only reach node 11 and 33. The constructed edges satisfy this constraint.

For the second test case, it can be proven that no possible solution exists.

对于第一个测试用例,节点 11 和 44 只能到达自身,节点 22 可以到达所有节点,节点 33 只能到达节点 11 和 33。所构造的边满足该约束条件。

对于第二个测试用例,可以证明不存在可行解。

输入解题思路,AI测评打分。不知道怎么写?

首页