This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);
#define what(x) cerr << #x << " is " << x << '\n';
#define kill(x) {cout << x << endl; return 0;}
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pb push_back
#define ll long long
#define F first
#define S second
const ll inf = 1e18, mod = 1e9 + 7, delta = 1e9 + 9, SQ = 450, P = 6065621;
using namespace std;
const int N = 2005, LG = 40;
vector<pair<int, pii>> candies;
int par[N][LG], h[N];
vector<int> adj[N];
bool b[N];
void dfs (int v, int p = -1) {
par[v][0] = p;
if (p + 1) h[v] = h[p] + 1;
for (int i = 1; i < LG; i++)
if (par[v][i] + 1)
par[v][i] = par[par[v][i - 1]][i - 1];
for (auto u: adj[v])
if (u - p) dfs(u, v);
}
int lca (int u, int v) {
if (h[u] < h[v]) swap(u, v);
for (int i = LG - 1; i >= 0; i--)
if (par[u][i] + 1 && h[par[u][i]] >= h[v])
u = par[u][i];
if (u == v) return v;
for (int i = LG - 1; i >= 0; i--)
if (par[u][i] != par[v][i])
v = par[v][i], u = par[u][i];
return par[v][0];
}
bool cmp (pair<int, pii> x, pair<int, pii> y) {
return x.F >= y.F;
}
int main () {
fast_io;
int n;
cin >> n;
memset(par, -1, sizeof par);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
vector<int> l;
for (int i = 1; i <= n; i++) {
if (adj[i].size() == 1)
l.pb(i);
}
for (int i = 0; i < l.size(); i++)
for (int j = i + 1; j < l.size(); j++) {
int u = l[i], v = l[j];
candies.pb({h[v] + h[u] - 2 * h[lca(u, v)], {u, v}});
}
sort(all(candies), cmp);
cout << l.size() / 2 + (l.size() % 2 == 1) << '\n';
set<int> s;
for (auto u: l) s.insert(u);
if (l.size() & 1) {
for (auto e: candies) {
if (b[e.S.F] || b[e.S.S]) continue;
cout << e.S.F << ' ' << e.S.S << '\n';
b[e.S.F] = b[e.S.S] = true;
s.erase(e.S.F);
s.erase(e.S.S);
}
int v = *s.begin();
for (auto e: candies) {
if (e.S.F == v || e.S.S == v) {
cout << e.S.F << ' ' << e.S.S << '\n';
break;
}
}
// cout << candies[0].S.F << ' ' << candies[0].S.S << '\n';
// b[candies[0].S.S] = true;
// for (auto e: candies) {
// if (b[e.S.F] || b[e.S.S]) continue;
// cout << e.S.F << ' ' << e.S.S << '\n';
// b[e.S.F] = b[e.S.S] = true;
// }
}
else {
for (auto e: candies) {
if (b[e.S.F] || b[e.S.S]) continue;
cout << e.S.F << ' ' << e.S.S << '\n';
b[e.S.F] = b[e.S.S] = true;
}
}
return 0;
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:65:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 0; i < l.size(); i++)
| ~~^~~~~~~~~~
net.cpp:66:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int j = i + 1; j < l.size(); j++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |