Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Do not try to search over trees. Pick a target square first, then design the tree to hit it.
For , the path works because
Try extending a working tree on vertices into one on vertices while changing the target from to .
The square increases by Adding vertex with edge contributes , so you still need an extra .
That extra comes from replacing edge by edge , because the contribution changes from to . So for all large , keep a fixed backbone and attach most middle vertices to .
We need construct a tree on vertices such that is a perfect square.
The clean move is to stop treating the square as mysterious. We will force the answer to be exactly for every .
Small cases first:
Now the real construction.
Base case
For , use the path
Its sum is
So works.
How to grow the tree
Suppose we already have a tree on vertices with sum .
We want a tree on vertices with sum .
The required increase is
Add the new vertex using edge . That contributes .
Still missing:
Now replace edge with edge . The contribution changes from to so the sum increases by exactly .
Perfect. No magic, just accounting. Trees are basically spreadsheets with delusions of grandeur.
Final explicit shape
Unrolling that growth gives a very simple construction for every :
This has exactly edges, so the count is right.
It is connected: vertices form a path, vertex is attached to , and every vertex from to is attached to .
It has no cycle: all the extra vertices are leaves, and the backbone is a tree. So it is definitely a tree.
Now compute the sum:
That is
Since we get
So
Exactly a square. Nice and boring, which is what you want in constructive problems.
Algorithm
For each testcase:
Complexity
We output edges, so the time is per testcase and extra memory.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n == 2) {
cout << -1 << '\n';
} else if (n == 3) {
cout << "1 3\n";
cout << "2 3\n";
} else if (n == 4) {
cout << "1 2\n";
cout << "1 3\n";
cout << "1 4\n";
} else {
cout << "1 2\n";
cout << "2 3\n";
cout << "3 4\n";
cout << 1 << ' ' << n << '\n';
for (int i = 5; i < n; ++i) {
cout << 2 << ' ' << i << '\n';
}
}
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n == 2) {
cout << -1 << '\n';
} else if (n == 3) {
cout << "1 3\n";
cout << "2 3\n";
} else if (n == 4) {
cout << "1 2\n";
cout << "1 3\n";
cout << "1 4\n";
} else {
cout << "1 2\n";
cout << "2 3\n";
cout << "3 4\n";
cout << 1 << ' ' << n << '\n';
for (int i = 5; i < n; ++i) {
cout << 2 << ' ' << i << '\n';
}
}
}
return 0;
}