Maximise the bridges solution codechef

Maximise the bridges solution codechef

Chef is given two integers NN and MM. Please help Chef find any connected undirected graph GG consisting of exactly NN vertices and MM edges, such that the number of bridges in GG is maximized (among all graphs with NN vertices and MM edges). GG cannot have self-loops or multiple edges.

If there is more than one connected undirected graph with the maximum number of bridges, you may print any one of them.

Note: A bridge is an edge whose removal increases the number of connected components of the graph.

Input Format

Maximise the bridges solution codechef

  • The first line of input contains a single integer TT, denoting the number of test cases. The description of TT test cases follows.
  • Each testcase consists of a single line of input, containing two integers N,MN,M – the number of vertices and edges of the graph you must construct.

Output Format

Maximise the bridges solution codechef

For each test case, output MM lines where the ii-th line contains two space-separated integers denoting the ii-th edge of the graph GG you constructed. The edges may be printed in any order, and for each edge the endpoints may be printed in any order.

Note that GG must not contain self-loops or multiple edges, and no other graph on NN vertices and MM edges can contain strictly more bridges than GG.

Constraints

  • 1T1031≤T≤103
  • 2N1032≤N≤103
  • N1Mmin(N(N1)/2,105)N−1≤M≤min(N⋅(N−1)/2,105)
  • Sum of NN over all test cases do not exceed 103103
  • Sum of MM over all test cases do not exceed 105105

Subtasks

Maximise the bridges solution codechef

  • Subtask 1 (100 points): Original constraints

Sample Input 1 

3
4 4
5 4
3 3

Sample Output 1

Maximise the bridges solution codechef

1 2
2 3
3 4
1 3
1 2
2 3
3 4
4 5
1 2
2 3
3 1

Explanation

Maximise the bridges solution codechef

Test case 11: There is exactly 11 bridge in the graph and it cannot be increased further.

Test case 22: Since M=N1M=N−1 and we are required to output a connected graph, the graph will always be a tree and hence have 44 bridges. Any tree is acceptable here.

Test case 33: Since M=N(N1)/2M=N⋅(N−1)/2 our only choice is a complete graph, which has 00 bridges.

Maximise the bridges solution codechef

Leave a Comment