This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// in the name of God
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fast() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mk make_pair
#define ppb pop_back
#define endl '\n'
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define file() freopen("input.txt", "r", stdin);
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O4")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7, maxn = 1e3 + 10, sq = 3163, inf = 1e18 + 10, lg = 25, pp = 4447, modd = 1e9 + 9;
int n;
vector<vector<int>> g(maxn), par(lg, vector<int> (maxn, -1));
int h[maxn], mark[maxn];
void dfs(int u = 0, int parr = -1){
par[0][u] = parr;
for(int i = 1; i < lg; ++i){
if(par[i - 1][u] < 0) break;
par[i][u] = par[i - 1][par[i - 1][u]];
}
for(int v : g[u]){
if(v != parr){
h[v] = h[u] + 1;
dfs(v, u);
}
}
}
int lca(int a, int b){
if(h[a] < h[b]) swap(a, b);
for(int i = lg - 1; i >= 0; --i){
if(par[i][a] > -1 && h[par[i][a]] > h[b]) a = par[i][a];
}
if(a == b) return a;
for(int i = lg - 1; i >= 0; --i){
if(par[i][a] > -1 && par[i][b] > -1 && par[i][a] != par[i][b]){
a = par[i][a];
b = par[i][b];
}
}
return par[0][a];
}
signed main(){
fast();//file();
cin >> n;
for(int i = 1; i < n; ++i){
int u, v; cin >> u >> v;
u--; v--;
g[u].pb(v);
g[v].pb(u);
}
dfs();
vector<int> b;
vector<pair<int, pii>> a;
for(int i = 0; i < n; ++i) if(g[i].size() == 1) b.pb(i);
for(int i = 0; i < b.size(); ++i){
for(int j = i + 1; j < b.size(); ++j){
int x = h[b[i]] + h[b[j]] - 2 * h[lca(b[i], b[j])];
a.pb(mk(x, mk(b[i], b[j])));
}
}
cout << ceil((double) b.size() / 2) << endl;
sort(all(a));
reverse(all(a));
for(int i = 0; i < a.size(); ++i){
int x = a[i].ff, y = a[i].ss.ff, z = a[i].ss.ss;
if(!mark[y] && !mark[z]){
mark[y] = true; mark[z] = true;
cout << y + 1 << " " << z + 1 << endl;
}
}
for(int i = 0; i < b.size(); ++i){
if(!mark[b[i]]){
cout << b[i] + 1 << " " << b.back() + 1;
}
}
}
// Running from the daylight...
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:67:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i = 0; i < b.size(); ++i){
| ~~^~~~~~~~~~
net.cpp:68:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int j = i + 1; j < b.size(); ++j){
| ~~^~~~~~~~~~
net.cpp:76:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i = 0; i < a.size(); ++i){
| ~~^~~~~~~~~~
net.cpp:77:7: warning: unused variable 'x' [-Wunused-variable]
77 | int x = a[i].ff, y = a[i].ss.ff, z = a[i].ss.ss;
| ^
net.cpp:83:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i = 0; i < b.size(); ++i){
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |