Circular Spanning Tree (Codeforces 1682D)
Tags: Others, trees

There are $$$n$$$ nodes arranged in a circle numbered from $$$1$$$ to $$$n$$$ in the clockwise order. You are also given a binary string $$$s$$$ of length $$$n$$$.Your task is to construct a tree on the given $$$n$$$ nodes satisfying the two conditions below or report that there such tree does not exist:  For each node $$$i$$$ $$$(1 \le i \le n)$$$, the degree of node is even if $$$s_i = 0$$$ and odd if $$$s_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)$$$ in the tree is drawn as a line segment connecting $$$u$$$ and $$$v$$$ on the circle.A tree on $$$n$$$ nodes is a connected graph with $$$n - 1$$$ edges.

Input: The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(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 $$$n$$$ $$$(2 \leq n \leq 2\cdot 10^5)$$$  — the number of nodes.The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

Output: 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 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 - 1$$$ lines, each containing two integers $$$u$$$ and $$$v$$$ $$$(1 \leq u,v \leq n, u \neq v)$$$ denoting an edge between $$$u$$$ and $$$v$$$ in the tree. If there are multiple possible answers, output any.

Note: 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 $$$1$$$ and $$$2$$$, 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.

Sample Inputs:
3
4
0110
2
10
6
110110

Sample Outputs:
YES
2 1
3 4
1 4
NO
YES
2 3
1 2
5 6
6 2
3 4

Solution (Python):
import sys

def solve():
    inp = sys.stdin.readline
    n = int(inp())
    s = inp()
    if s.count('1') % 2 != 0 or s.count('1') == 0:
        print('No')
        return
    ones = []
    p = None
    edges = []
    for j in range(2):
        for i in range(n):
            if s[i] == '1':
                if p is not None:
                    ones.append(p)
                if j:
                    break
                p = i
            elif p is not None:
                edges.append(str(p + 1) + ' ' + str(i + 1))
                p = i
    for i in range(1, len(ones)):
        edges.append(str(ones[0] + 1) + ' ' + str(ones[i] + 1))
    print('Yes')
    print('\n'.join(edges))

def main():
    for i in range(int(sys.stdin.readline())):
        solve()
if __name__ == '__main__':
    main()

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    s = input().strip()
    if s.count('1') == 0 or s.count('1') % 2 == 1:
        print('NO')
        continue
    L = []
    edges = []
    for i in range(n):
        if s[i] == '1':
            L.append((i + 1) % n)
        else:
            edges.append(str(i + 1) + ' ' + str((i + 1) % n + 1))
    x = L[0]
    for i in range(1, len(L)):
        edges.append(str(x + 1) + ' ' + str(L[i] + 1))
    print('YES')
    print('\n'.join(edges))

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    s = input().strip()
    if s.count('1') == 0 or s.count('1') % 2 == 1:
        print('NO')
        continue
    begins = []
    edges = []
    for i in range(n):
        if s[i] == '1':
            begins.append((i + 1) % n)
        else:
            edges.append((i, (i + 1) % n))
    b = begins[0]
    edges += [(b, begins[j]) for j in range(1, len(begins))]
    print('YES')
    print('\n'.join((f'{x + 1} {y + 1}' for (x, y) in edges)))