CF1696F.Tree Recovery

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Fishingprince loves trees. A tree is a connected undirected graph without cycles.

Fishingprince has a tree of nn vertices. The vertices are numbered 11 through nn. Let d(x,y)d(x,y) denote the shortest distance on the tree from vertex xx to vertex yy, assuming that the length of each edge is 11.

However, the tree was lost in an accident. Fortunately, Fishingprince still remembers some information about the tree. More specifically, for every triple of integers x,y,zx,y,z (1≤x<y≤n1\le x \lt y\le n, 1≤z≤n1\le z\le n) he remembers whether d(x,z)=d(y,z)d(x,z)=d(y,z) or not.

Help him recover the structure of the tree, or report that no tree satisfying the constraints exists.

Fishingprince 非常喜欢树。树是一种无环的连通无向图。

Fishingprince 拥有一棵包含 nn 个顶点的树,顶点编号为 11 到 nn。记 d(x,y)d(x,y) 表示树上顶点 xx 到顶点 yy 的最短距离(假设每条边的长度均为 11)。

然而,这棵树在一次事故中丢失了。幸运的是,Fishingprince 仍记得关于该树的一些信息。具体而言,对于每个三元组整数 (x,y,z)(x,y,z)(满足 1≤x<y≤n1\le x \lt y\le n 且 1≤z≤n1\le z\le n),他记得 d(x,z)=d(y,z)d(x,z)=d(y,z) 是否成立。

请帮助他恢复该树的结构;若不存在满足所有约束条件的树,则报告无解。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2001 \le t \le 200). Description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤1002\le n\le 100) — the number of vertices in the tree.

Then n−1n-1 lines follow. The ii-th line of these n−1n-1 lines contains n−in-i strings of length nn consisting of 0 and 1. If the kk-th character in the jj-th string of the ii-th line is 0, it means that d(i,k)≠d(i+j,k)d(i,k)\ne d(i+j,k); if the kk-th character in the jj-th string of the ii-th line is 1, it means that d(i,k)=d(i+j,k)d(i,k)=d(i+j,k).

It is guaranteed that in one input file,

  • there are at most 22 test cases that have n>50n \gt 50;
  • there are at most 55 test cases that have n>20n \gt 20.

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

每个测试用例的第一行包含一个整数 nn(2≤n≤1002\le n\le 100)——树中顶点的数量。

接下来是 n−1n-1 行。这 n−1n-1 行中的第 ii 行包含 n−in-i 个长度为 nn 的字符串,每个字符串仅由字符 0 和 1 组成。若第 ii 行中第 jj 个字符串的第 kk 个字符为 0,表示 d(i,k)≠d(i+j,k)d(i,k)\ne d(i+j,k);若该字符为 1,则表示 d(i,k)=d(i+j,k)d(i,k)=d(i+j,k)。

保证在单个输入文件中:

  • 满足 n>50n \gt 50 的测试用例至多有 22 个;
  • 满足 n>20n \gt 20 的测试用例至多有 55 个。

输出格式

For each test case:

  • if no answer exists, output No;
  • otherwise, on the first line output Yes. Then output n−1n-1 lines. Each line should contain two integers x,yx,y (1≤x,y≤n1\le x,y\le n), denoting an edge between vertices xx and yy of the tree. If there are multiple solutions, print any.

When printing Yes and No, you can print each letter in any case (upper or lower).

对于每个测试用例:

  • 如果不存在答案,输出 No;
  • 否则,在第一行输出 Yes。然后输出 n−1n-1 行,每行包含两个整数 x,yx,y(1≤x,y≤n1\le x,y\le n),表示树中顶点 xx 与 yy 之间的一条边。如果存在多个解,输出任意一个即可。

在输出 Yes 和 No 时,每个字母可以使用任意大小写(大写或小写)。

输入输出样例

  • 输入#1

    5
    2
    00
    2
    10
    3
    001 000
    000
    3
    001 010
    000
    5
    00000 01001 00000 01100
    00000 10000 00000
    00000 11010
    00000

    输出#1

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

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

首页