This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/// ITNOG
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define fast_io ios::sync_with_stdio(false);cin.tie(NULL);
#define file_io freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define FOR(i,k,n) for(int i = k; i < n; ++ i)
#define debf cout<<"(0-0)\n";
#define all(x) x.begin(), x.end()
#define dec(x) cout << fixed << setprecision(x);
#define pf push_front
#define ppf pop_front
#define dash " ------- "
#define what(x) cerr << #x << " is " << x << endl;
#define eb emplace_back
//#define int short int
#define int long long
#define sz(s) (int) (s.size())
#define fl cout.flush()
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
typedef unsigned long long ull;
typedef long double ld;
template <class T> using max_heap = priority_queue <T, vector <T>, less <T> >;
template <class T> using min_heap = priority_queue <T, vector <T>, greater <T> >;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
constexpr int MOD = 1e9 + 7, N = 5e5 + 8, M = 1e6, SQ = 600, INF = 1e15 + 8, LGN = 22, mod = 998244353, P = 131113;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, leaf, subleaf[N], r1;
vector <int> adj[N];
vector <int> v[N];
vector <pii> ans;
max_heap <pii> pq;
void dfs (int u, int par = -1){
if (adj[u].size() == 1){
++ leaf;
++ subleaf[u];
}
for (int i : adj[u]){
if (i != par){
dfs (i, u);
subleaf[u] += subleaf[i];
}
}
return;
}
void dfs2 (int u, int par = -1){
for (int i = 0; i < adj[u].size(); ++ i){
int x = adj[u][i];
if (x != par){
if (subleaf[x] > leaf / 2){
i = INF;
r1 = x;
dfs2 (x, u);
}
}
}
return;
}
void dfs3 (int u, int xx = -1, int par = -1){
if (adj[u].size() == 1){
v[xx].pb(u);
}
for (int i : adj[u]){
if (i != par){
if (u == r1){
dfs3 (i, i, u);
}
else {
dfs3 (i, xx, u);
}
}
}
return;
}
int32_t main(){
fast_io;
cin >> n;
FOR (i, 0, n - 1){
int aa, bb;
cin >> aa >> bb;
adj[--aa].pb(--bb);
adj[bb].pb(aa);
if (adj[aa].size() > 1){
r1 = aa;
}
}
dfs (r1);
dfs2 (r1);
cout << (leaf + 1) / 2 << '\n';
dfs3 (r1);
for (int i = 0; i < n; ++ i){
if (v[i].size()){
pq.push({v[i].size(), i});
}
}
while (pq.size()){
if (pq.size() == 1){
break;
}
pii p1 = pq.top();
pq.pop();
pii p2 = pq.top();
pq.pop();
ans.pb({v[p1.S].back(), v[p2.S].back()});
v[p1.S].ppb();
v[p2.S].ppb();
-- p1.F;
-- p2.F;
if (p1.F){
pq.push(p1);
}
if (p2.F){
pq.push(p2);
}
}
if (pq.size() == 1){
ans.pb({v[pq.top().S].back(), r1});
}
for (pii i : ans){
cout << i.F + 1 << " " << i.S + 1 << '\n';
}
return 0;
}
// Yesterday is history
// Tomorrow is a mystery
// but today is a gift
// That is why it is called the present
Compilation message (stderr)
net.cpp: In function 'void dfs2(long long int, long long int)':
net.cpp:67:21: 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 < adj[u].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... |