Districts Connection (Codeforces 1433D)
Tags: Others, dfs and similar

There are $$$n$$$ districts in the town, the $$$i$$$-th district belongs to the $$$a_i$$$-th bandit gang. Initially, no districts are connected to each other.You are the mayor of the city and want to build $$$n-1$$$ two-way roads to connect all districts (two districts can be connected directly or through other connected districts).If two districts belonging to the same gang are connected directly with a road, this gang will revolt.You don't want this so your task is to build $$$n-1$$$ two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build $$$n-1$$$ roads to satisfy all the conditions.You have to answer $$$t$$$ independent test cases.

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then $$$t$$$ test cases follow.The first line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 5000$$$) — the number of districts. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the gang the $$$i$$$-th district belongs to.It is guaranteed that the sum of $$$n$$$ does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$).

Output: For each test case, print:  NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.  YES on the first line and $$$n-1$$$ roads on the next $$$n-1$$$ lines. Each road should be presented as a pair of integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n; x_i \ne y_i$$$), where $$$x_i$$$ and $$$y_i$$$ are two districts the $$$i$$$-th road connects. For each road $$$i$$$, the condition $$$a[x_i] \ne a[y_i]$$$ should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).


Sample Inputs:
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4

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

Solution (Python):
num_test_cases = int(input())
for _ in range(num_test_cases):
    num_districts = int(input())
    gangs = list(map(int, input().split()))
    if len(set(gangs)) == 1:
        print('NO')
        continue
    print('YES')
    index = -1
    for i in range(1, num_districts):
        if gangs[i] != gangs[0]:
            print(1, i + 1)
            index = i
    for i in range(1, num_districts):
        if gangs[i] == gangs[0]:
            print(index + 1, i + 1)

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    ans = []
    vis = {1}
    queue = [1]
    while queue:
        curr = queue.pop()
        for nxt in range(0, n):
            if arr[curr] == arr[nxt]:
                continue
            if nxt not in vis:
                vis.add(nxt)
                queue.append(nxt)
                ans.append(f'{curr + 1} {nxt + 1}')
        if len(ans) == n - 1:
            break
    if not ans:
        print('NO')
    else:
        print('YES')
        print(*ans, sep='\n')

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    edges = {}
    ans = []
    vis = set()
    vis.add(1)
    queue = [1]
    while queue:
        curr = queue.pop(0)
        for nxt in range(0, n):
            if arr[curr] == arr[nxt]:
                continue
            if nxt not in vis:
                vis.add(nxt)
                queue.append(nxt)
                ans.append(f'{curr + 1} {nxt + 1}')
        if len(ans) == n - 1:
            break
    if not ans:
        print('NO')
    else:
        print('YES')
        print(*ans, sep='\n')
exit()
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    queue = []
    roads = []
    root = 1
    connected = {1}
    while True:
        for i in range(0, n):
            if l[root - 1] == l[i]:
                continue
            elif i + 1 not in connected:
                roads.append((root, i + 1))
                queue.append(i + 1)
                connected.add(i + 1)
        if len(queue) == 0 or len(connected) == n:
            break
        root = queue.pop(0)
    if len(roads) == 0:
        print('NO')
    else:
        print('YES')
        for (i, j) in roads:
            print(i, j)