# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
399530 | cpp219 | Network (BOI15_net) | C++14 | 3 ms | 768 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 5e3 + 9;
const ll inf = 1e9 + 7;
const ll Log2 = 21;
vector<ll> g[N];
deque<ll> leaf;
vector<LL> ans;
ll n,x,y,root,pos[N],d[N],par[N][Log2];
ll now;
void DFS(ll u,ll p){
pos[u] = now; now++;
for (ll i = 1;i < Log2;i++) if ((1 << u) <= d[u])
par[u][i] = par[par[u][i - 1]][i - 1];
for (auto i : g[u]){
if (i != p){
d[i] = d[u] + 1; par[i][0] = u;
DFS(i,u);
}
}
}
bool lf(ll x,ll y){
return pos[x] < pos[y];
}
void out(){
for (auto i : leaf) cout<<i<<" "; exit(0);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
}
cin>>n;
for (ll i = 1;i < n;i++){
cin>>x>>y;
g[x].push_back(y); g[y].push_back(x);
}
for (ll i = 1;i <= n;i++){
if (g[i].size() > 1) root = i;
else leaf.push_back(i);
}
DFS(root,0); sort(leaf.begin(),leaf.end(),lf);
ll sz = (leaf.size() + 1) / 2,inc = leaf.size()/2;
cout<<sz<<"\n";
for (ll i = 0;i < sz;i++) cout<<leaf[i]<<" "<<leaf[i + inc]<<"\n";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |