G. Beautiful Tree
Problem Statement
A tree is a connected graph without cycles.
A tree is called beautiful if the sum of the products of the vertex labels for all its edges is a perfect square.
More formally, let be the set of edges in the tree. The tree is called beautiful if the value is a perfect square. That is, there exists an integer such that .
You are given an integer . Your task is to construct a beautiful tree having vertices or report that such a tree does not exist.
Solution Explanation
The problem asks us to construct a tree with vertices such that the sum of products of adjacent vertex labels is a perfect square.
Let’s analyze the sum . We need to find a tree structure and a labeling of vertices from to that makes a perfect square. A simple tree structure to analyze is a star graph, where one central vertex is connected to all other vertices.
Let’s try to construct a solution around a target sum that is a perfect square. A natural candidate for a perfect square that is related to is . Can we achieve a sum of ?
Consider a small case. If we connect vertex to , and vertex to , and all other vertices (where ) to vertex . The sum of edge products would be: This seems complicated.
Let’s try a different construction for . We create the edges (1, n), (1, 2), (2, 3), and (3, 4). Then, we connect all remaining vertices to vertex 2. The sum of edge products is: Let’s analyze the sum. . This construction gives a sum of , which is a perfect square.
This construction works for . We need to handle the small cases separately:
- n = 2: The only possible tree is an edge between 1 and 2. The sum is , which is not a perfect square. No solution exists.
- n = 3: We can connect (1,3) and (2,3). The sum is . This is a beautiful tree.
- n = 4: We can connect (1,2), (1,3), (1,4). The sum is . This is a beautiful tree.
- n = 5: We can connect (1,5), (1,2), (2,3), (3,4). The sum is . This is a beautiful tree.
For , the construction is:
- Edge (1, n)
- Edge (1, 2)
- Edge (2, 3)
- Edge (3, 4)
- For all from 5 to , add edge (2, i).
The provided code implements these specific constructions for each case.
View Code
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
#define int long long
// #define mod 1000000007
#define N 500005
void solve() {
int n;
cin >> n;
if (n == 2) {
cout << -1 <<endl;
return ;
}
if (n == 3) {
cout << "1 3\n2 3\n" <<endl;
return ;
}
if (n == 4) {
cout << "1 2\n3 1\n 4 1\n";
return;
}
if (n == 5) {
cout << "1 5\n1 2\n2 3\n3 4\n";
return;
}
cout << "1 "<< n << "\n1 2\n2 3\n3 4\n";
for(int i = 5; i < n; i++) {
cout << 2 << " " << i << "\n";
}
cout << endl;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef DEBUG
freopen("./input.txt", "r", stdin);
#endif
int t;
cin >> t;
while (t--) {
#ifdef DEBUG
static int test_num = 1;
cout << "test case: " << test_num++ << endl;
#endif
solve();
}
}