CF1919G.Tree LGM

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

In TreeWorld, there is a popular two-player game played on a tree with nn vertices labelled from 11 to nn. In this game, the tournament leaders first choose a vertex to be the root of the tree and choose another vertex (possibly the same vertex as the root) to place a coin on. Then, each player will take turns moving the coin to any child†^\dagger of the vertex that the coin is currently on. The first player who is unable to make a move loses.

Alice wants to be a tree LGM, so she spends a lot of time studying the game. She wrote down an nn by nn matrix ss, where si,j=1s_{i,j} = \mathtt{1} if the first player can win with the root of the tree chosen to be vertex ii, and the coin was initially placed on vertex jj. Otherwise, si,j=0s_{i, j} = \mathtt{0}. Alice is a perfectionist, so she assumes that both players play perfectly in the game.

However, she accidentally knocked her head on the way to the tournament and forgot what the tree looked like. Determine whether there exists a tree that satisfies the winning and losing states represented by matrix ss, and if it exists, construct a valid tree.

†^\dagger A vertex cc is a child of vertex uu if there is an edge between cc and uu, and cc does not lie on the unique simple path from the root to vertex uu.

在 TreeWorld 中,有一种流行的双人游戏,在一棵含有 nn 个顶点(编号为 11 到 nn)的树上进行。游戏中,比赛组织者首先选定一个顶点作为树的根,并在另一个顶点(可以与根相同)上放置一枚硬币。随后,双方玩家轮流将硬币沿树边移动到当前硬币所在顶点的任意一个子节点†^\dagger。第一个无法进行移动的玩家判负。

Alice 希望成为树类 LGM(Legendary Grandmaster),因此她花费大量时间研究该游戏。她记录下了一个 n×nn \times n 的矩阵 ss,其中当以顶点 ii 为根、硬币初始置于顶点 jj 时,若先手玩家有必胜策略,则 si,j=1s_{i,j} = \mathtt{1};否则 si,j=0s_{i,j} = \mathtt{0}。Alice 是一名完美主义者,因此她假设双方均采用最优策略进行游戏。

然而,她在前往比赛途中不慎撞到了头,忘记了这棵树的具体结构。请判断是否存在一棵树,使得其对应的胜负状态恰好由矩阵 ss 所表示;若存在,请构造出这样的一棵合法树。

†^\dagger 若顶点 cc 与顶点 uu 之间存在一条边,且 cc 不在从根到顶点 uu 的唯一简单路径上,则称顶点 cc 是顶点 uu 的一个子节点。

输入格式

The first line contains a single integer nn (1≤n≤50001 \le n \le 5000) — the number of vertices in the tree.

Each of the next nn lines contains a string with nn characters, the jj-th character of the ii-th line representing si,js_{i, j} (si,j∈0,1s_{i, j} \in {\mathtt{0}, \mathtt{1}}) — the winning and losing states of the tree.

第一行包含一个整数 nn(1≤n≤50001 \le n \le 5000)——树中顶点的数量。

接下来的 nn 行,每行包含一个长度为 nn 的字符串;其中第 ii 行的第 jj 个字符表示 si,js_{i, j}(si,j∈{0,1}s_{i, j} \in \{\mathtt{0}, \mathtt{1}\})——树的胜负状态。

输出格式

If there is no tree satisfying the winning and losing states represented by matrix ss, print a single line containing "NO".

Otherwise, if there exists a tree satisfying matrix ss, print "YES" on the first line, followed by n−1n - 1 lines each containing two integers uu and vv (1≤u,v≤n1 \le u, v \le n) representing that the tree has an edge between vertices uu and vv.

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.

If there are multiple trees satisfying the winning and losing states represented by matrix ss, print any of them.

如果不存在满足矩阵 ss 所表示的胜负状态的树,则输出一行,内容为 "NO"。

否则,若存在满足矩阵 ss 的树,则第一行输出 "YES",随后输出 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n),表示该树中存在连接顶点 uu 和 vv 的一条边。

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

若存在多个满足矩阵 ss 所表示的胜负状态的树,输出其中任意一个即可。

输入输出样例

  • 输入#1

    4
    1100
    0101
    0011
    0101

    输出#1

    YES
    4 1
    3 2
    2 4
  • 输入#2

    3
    001
    010
    100

    输出#2

    NO

说明/提示

In the first test case, the line graph 1 ⁣− ⁣4 ⁣− ⁣2 ⁣− ⁣31\!-\!4\!-\!2\!-\!3 satisfies the winning and losing states represented by matrix ss. For example, s3,3=1s_{3,3} = 1 as the first player can move the coin from 3→23\rightarrow 2, then the second player moves the coin from 2→42\rightarrow 4, and finally, the first player moves the coin from 4→14\rightarrow 1. At this point, 11 has no children, so the second player is unable to make a move and loses. On the other hand, s1,3=0s_{1,3} = 0 as if 11 is the root, then 33 has no children so the first player is unable to make the first move and loses.

In the second test case, it is possible to prove that no tree satisfies the winning and losing states represented by matrix ss.

在第一个测试用例中,路径图 1 ⁣− ⁣4 ⁣− ⁣2 ⁣− ⁣31\!-\!4\!-\!2\!-\!3 满足矩阵 ss 所表示的胜负状态。例如,s3,3=1s_{3,3} = 1,因为先手玩家可将硬币从 3→23\rightarrow 2 移动,随后后手玩家将硬币从 2→42\rightarrow 4 移动,最后先手玩家将硬币从 4→14\rightarrow 1 移动。此时节点 11 没有子节点,因此后手玩家无法进行下一步操作而失败。另一方面,s1,3=0s_{1,3} = 0,因为若以 11 为根节点,则节点 33 没有子节点,故先手玩家无法执行首次移动而失败。

在第二个测试用例中,可以证明:不存在任何树能满足矩阵 ss 所表示的胜负状态。

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

首页