CF1682D.Circular Spanning Tree

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn nodes arranged in a circle numbered from 11 to nn in the clockwise order. You are also given a binary string ss of length nn.

Your task is to construct a tree on the given nn nodes satisfying the two conditions below or report that such tree does not exist:

  • For each node ii (1≤i≤n)(1 \le i \le n), the degree of node is even if si=0s_i = 0 and odd if si=1s_i = 1.
  • No two edges of the tree intersect internally in the circle. The edges are allowed to intersect on the circumference.

Note that all edges are drawn as straight line segments. For example, edge (u,v)(u, v) in the tree is drawn as a line segment connecting uu and vv on the circle.

A tree on nn nodes is a connected graph with n−1n - 1 edges.

有 nn 个节点按顺时针顺序围成一个圆圈,编号为 11 到 nn。同时给定一个长度为 nn 的二进制字符串 ss。

你的任务是:在给定的 nn 个节点上构造一棵树,使其满足以下两个条件;若不存在这样的树,则报告无解:

  • 对每个节点 ii(1≤i≤n1 \le i \le n),其度数为偶数当且仅当 si=0s_i = 0,为奇数当且仅当 si=1s_i = 1;
  • 树中任意两条边在圆内部不相交(但允许在圆周上相交)。

注意:所有边均以直线段形式绘制。例如,树中的一条边 (u,v)(u, v) 即为圆上连接节点 uu 和 vv 的直线段。

一棵含 nn 个节点的树是一个具有 n−1n - 1 条边的连通图。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤2⋅104)(1 \leq t \leq 2\cdot 10^4) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅105)(2 \leq n \leq 2\cdot 10^5) — the number of nodes.

The second line of each test case contains a binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

输入包含多个测试用例。第一行包含一个整数 tt (1≤t≤2⋅104)(1 \leq t \leq 2\cdot 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn (2≤n≤2⋅105)(2 \leq n \leq 2\cdot 10^5),表示节点数量。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, if there does not exist a tree that satisfies the given conditions, then output "NO" (without quotes), otherwise output "YES" followed by the description of a suitable tree.

You can output each letter in any case (for example, "YES", "Yes", "yes", "yEs", "yEs" will be recognized as a positive answer).

If there exists a tree, then output n−1n - 1 lines, each containing two integers uu and vv (1≤u,v≤n,u≠v)(1 \leq u,v \leq n, u \neq v) denoting an edge between uu and vv in the tree. If there are multiple possible answers, output any.

对于每个测试用例,若不存在满足给定条件的树,则输出 "NO"(不带引号);否则输出 "YES",后跟一棵符合条件的树的描述。

你可以以任意大小写形式输出每个字母(例如 "YES"、"Yes"、"yes"、"yEs"、"yEs" 均被视为肯定回答)。

如果存在这样的树,则输出 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \leq u,v \leq n,且 u≠vu \neq v),表示树中连接顶点 uu 和 vv 的一条边。若存在多种可能的答案,输出任意一种即可。

输入输出样例

  • 输入#1

    3
    4
    0110
    2
    10
    6
    110110

    输出#1

    YES
    2 1
    3 4
    1 4
    NO
    YES
    2 3
    1 2
    5 6
    6 2
    3 4

说明/提示

In the first test case, the tree looks as follows:

In the second test case, there is only one possible tree with an edge between 11 and 22, and it does not satisfy the degree constraints.

In the third test case,

The tree on the left satisfies the degree constraints but the edges intersect internally, therefore it is not a valid tree, while the tree on the right is valid.

在第一个测试用例中,树的结构如下所示:

在第二个测试用例中,仅存在一种可能的树,即节点 11 与节点 22 之间有一条边;但该树不满足度数约束。

在第三个测试用例中,

左侧的树满足度数约束,但其边在内部相交,因此不是一棵合法的树;而右侧的树是合法的。

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

首页