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];
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;
for(int i = 0; i < n; ++i) if(g[i].size() == 1) b.pb(i);
for(int i = 0; i < b.size() - 2; ++i){
if(h[lca(b[i], b[i + 1])] <= h[lca(b[i + 1], b[i + 2])]) swap(b[i + 1], b[i + 2]);
}
if(b.size() % 2){
cout << b.size() / 2 + 1 << endl << b[0] + 1 << " " << b.back() + 1 << endl;
for(int i = 0; i < b.size() - 1; i += 2){
cout << b[i] + 1 << " " << b[i + 1] + 1 << endl;
}
}
else{
cout << b.size() / 2 << endl;
for(int i = 0; i < b.size(); i += 2){
cout << b[i] + 1 << " " << b[i + 1] + 1 << endl;
}
}
}
// Running from the daylight...
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:66: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]
66 | for(int i = 0; i < b.size() - 2; ++i){
| ~~^~~~~~~~~~~~~~
net.cpp:71:20: 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]
71 | for(int i = 0; i < b.size() - 1; i += 2){
| ~~^~~~~~~~~~~~~~
net.cpp:77:20: 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]
77 | for(int i = 0; i < b.size(); i += 2){
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |