Max Graph Codechef solutions Today | Codechef Starters 72 ✅ (100/100) FULL | AC Code ✅| MAXGRAPH
Problem
Given an array of size , construct a simple (i.e, without self-loops and multiedges) strongly connected directed graph with nodes where denotes the colour of node.
The graph should satisfy the following properties:
- If , then nodes and should have the same outdegree.
- If , neither of the edges and should exist.
- If the edge exists, then the edge cannot exist.
- Under the above conditions, the number of edges should be maximized.
Note:
- It is guaranteed that contains at least distinct elements.
- If contains distinct elements, they will be the integers .
Find any graph that satisfies the above conditions.
If no such graph exists, print instead.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of two lines of input.
- The first line of each test consists of a single integer , denoting the number of nodes in the graph.
- The second line contains space-separated integers , where denotes the colour of -th node.
Output Format
For each test case,
- If no valid graph exists, output on a new line.
- Otherwise, first output on a new line : the maximum number of edges in such a graph.
- Then, output lines, each containing two space-separated integers and , denoting an edge from to .
Constraints
- , where is the number of distinct elements in .
- The sum of over all test cases won't exceed .
Sample 1:
Input
Output
3 4 1 0 1 0 5 0 1 2 3 4 3 0 1 1
4 1 2 3 4 4 1 2 3 10 1 2 2 3 2 4 2 5 3 1 3 4 3 5 4 1 4 5 5 1 -1
Explanation:
Test case : Nodes and are colored , and nodes and are colored .
The out degree of nodes is and out degree of nodes is also , and the graph is strongly connected.
Test case : Every node has a distinct color, so those conditions are automatically satisfied. edges is also the maximum possible number of edges such a graph can have.