├── Code(python).py ├── code(pypy3).pypy3 ├── Code(JAVA).java ├── Code(C++).cpp ├── Problem Explanation.txt └── Algorithm.txt /Code(python).py: -------------------------------------------------------------------------------- 1 | 2 | h = int(input()) 3 | 4 | for i in range(h): 5 | n, k = map(int, input().split()) 6 | p = n * (n - 1) // 2 7 | if k == p: 8 | print(k) 9 | for j in range(1,n): 10 | for z in range(j + 1, n + 1): 11 | print(j, z) 12 | elif k < p and (p - k) >= (n - 1): 13 | r = p - k 14 | print(r) 15 | for j in range(1, n): 16 | for z in range(j + 1, n + 1): 17 | if r != 0: 18 | print(j, z) 19 | r -= 1 20 | else: 21 | print(-1) -------------------------------------------------------------------------------- /code(pypy3).pypy3: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | n,k=[int(x) for x in input().split()] 3 | if k==(n*(n-1))//2: 4 | print((n*(n-1))//2) 5 | for i in range(1,n+1): 6 | for j in range(i+1,n+1): 7 | print(i,j) 8 | elif k<=((n-1)*(n-2))//2: 9 | print(n-1+((n-1)*(n-2))//2-k) 10 | for i in range(2,n+1): 11 | print(1,i) 12 | ctr=((n-1)*(n-2))//2-k 13 | for i in range(2,n+1): 14 | if ctr==0: break 15 | for j in range(i+1,n+1): 16 | if ctr==0: break 17 | print(i,j) 18 | ctr-=1 19 | else: 20 | print(-1) -------------------------------------------------------------------------------- /Code(JAVA).java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | /* Name of the class has to be "Main" only if the class is public. */ 6 | class Codechef 7 | { 8 | public static void main (String[] args) throws java.lang.Exception 9 | { 10 | Scanner s = new Scanner(System.in); 11 | int t=s.nextInt(); 12 | for(int x=0;x max && k!=(n*(n-1)/2)) 17 | System.out.println(-1); 18 | else if(k==(n*(n-1)/2)){ 19 | System.out.println(k); 20 | for(int i=1;i<=n;i++){ 21 | for(int j=i+1;j<=n;j++){ 22 | System.out.println(i+" "+j); 23 | } 24 | } 25 | } 26 | else{ 27 | int count=0; 28 | System.out.println(max-k+(n-1)); 29 | for(int i=1;ik){ 36 | System.out.println(i+" "+j); 37 | } 38 | } 39 | } 40 | } 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Code(C++).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | vector> solve(int n, int K) { 7 | vector> res; 8 | if (K == n * (n - 1) / 2) { 9 | for (int i = 0; i < n; ++i) { 10 | for (int j = i + 1; j < n; ++j) { 11 | res.push_back({i, j}); 12 | } 13 | } 14 | return res; 15 | } 16 | 17 | if (K > n * (n - 1) / 2 - (n - 1)) { 18 | return {}; 19 | } 20 | 21 | for (int i = 1; i < n; ++i) { 22 | res.push_back({0, i}); 23 | } 24 | 25 | int rem = (n - 1) * (n - 2) / 2 - K; 26 | for (int i = 1; i < n && rem > 0; ++i) { 27 | for (int j = i + 1; j < n && rem > 0; ++j) { 28 | res.push_back({i, j}); 29 | --rem; 30 | } 31 | } 32 | 33 | return res; 34 | } 35 | 36 | int main(int argc, char** argv) { 37 | ios::sync_with_stdio(false); 38 | cin.tie(0); 39 | cout << fixed << setprecision(12); 40 | 41 | 42 | int t; 43 | cin >> t; 44 | while (t-- > 0) { 45 | int n, K; 46 | cin >> n >> K; 47 | auto res = solve(n, K); 48 | if (res.empty()) { 49 | cout << -1 << '\n'; 50 | } else { 51 | cout << (int) res.size() << '\n'; 52 | for (auto [u, v] : res) { 53 | cout << u + 1 << ' ' << v + 1 << '\n'; 54 | } 55 | } 56 | } 57 | 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Problem Explanation.txt: -------------------------------------------------------------------------------- 1 | The problem statement presents a scenario where you need to construct a connected undirected unweighted graph with N nodes such that there are exactly K unordered pairs of nodes at a distance equal to the diameter of the graph. The diameter of a graph is the largest distance between any two nodes in the graph. 2 | 3 | The input consists of multiple test cases, where each test case contains two integers N and K. You are required to process each test case separately. 4 | 5 | The output should provide the following information: 6 | 7 | 1. If it is not possible to construct a valid graph, output -1. 8 | 2. If a valid graph exists, output the number of edges in the graph (M) on a separate line. 9 | 3. Next, output M lines, each containing two space-separated integers u and v, representing the endpoints of an edge in the graph. 10 | 11 | To solve the problem, you need to come up with a graph construction algorithm that satisfies the given conditions. The constructed graph should have N nodes and exactly K unordered pairs of nodes at the diameter distance. 12 | 13 | The example provided demonstrates the construction of a graph with N = 6 and K = 4. The output shows that the graph has 7 edges, and it lists the endpoints of each edge. 14 | 15 | The solution algorithm needs to ensure that the constructed graph fulfills the requirements stated in the problem. The graph should be connected, undirected, unweighted, and have the specified number of unordered pairs at the diameter distance. 16 | 17 | You will need to implement a solution that follows the problem constraints and generates the required output for each test case. -------------------------------------------------------------------------------- /Algorithm.txt: -------------------------------------------------------------------------------- 1 | Let's go through the code step by step to understand its functionality: 2 | 3 | 1. The code begins by reading an integer value from the input, which is stored in the variable `h`. This value represents the number of test cases that will follow. 4 | 5 | 2. The code then enters a loop that will execute `h` times. This loop is used to process each test case separately. 6 | 7 | 3. For each test case, the code reads two integers `n` and `k` from the input. These values represent the number of vertices in a graph and the number of edges to be added to the graph, respectively. 8 | 9 | 4. The variable `p` is calculated as `n * (n - 1) // 2`. This formula represents the maximum number of edges that can be present in a complete graph with `n` vertices. It is derived from the fact that each vertex can be connected to all other vertices except itself, resulting in `n - 1` edges per vertex. Since there are `n` vertices in total, the total number of edges is `(n - 1) + (n - 2) + ... + 1`, which is equivalent to `n * (n - 1) // 2`. 10 | 11 | 5. The code now checks three conditions using if-elif-else statements: 12 | 13 | a. If `k` is equal to `p`, it means that the given number of edges (`k`) is equal to the maximum number of edges (`p`) for a complete graph. In this case, the code prints the value of `k` and proceeds to print all possible edges of the complete graph. The edges are printed using nested loops that iterate over all pairs of vertices (j, z), where j ranges from 1 to n-1 and z ranges from j+1 to n. This ensures that each edge is printed only once. 14 | 15 | b. If `k` is less than `p` and the difference between `p` and `k` is greater than or equal to `n - 1`, it means that it is possible to add `k` edges to the graph while still keeping it connected. In this case, the code calculates the number of additional edges required, which is `r = p - k`. It then prints the value of `r` and proceeds to print `r` edges that are missing in the complete graph. The missing edges are printed using nested loops similar to the previous case, but the printing is stopped once `r` edges have been printed. 16 | 17 | c. If none of the above conditions are met, it means that it is not possible to add `k` edges to the graph while keeping it connected. In this case, the code prints `-1` to indicate that the input is invalid. 18 | 19 | 6. The loop continues to the next iteration, and the process is repeated for the remaining test cases. 20 | 21 | --------------------------------------------------------------------------------