# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
125052 | wilwxk | Network (BOI15_net) | C++14 | 649 ms | 77324 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.
#include <bits/stdc++.h>
using namespace std;
const int MAXN=5e5+5;
vector<int> g[MAXN];
int leave[MAXN][2];
vector<pair<int, int> > respf;
int n, raiz;
void dfs(int cur, int p) {
if(g[cur].size()==1) leave[cur][0]=cur;
if(g[cur].size()==1) return;
for(auto viz : g[cur]) {
if(viz==p) continue;
dfs(viz, cur);
if(!leave[cur][0]) {
leave[cur][0]=leave[viz][0];
leave[cur][1]=leave[viz][1];
continue;
}
if(leave[cur][1]) {
respf.push_back({leave[cur][1], leave[viz][0]});
leave[cur][1]=leave[viz][1];
}
else {
swap(leave[cur][0], leave[viz][0]);
if(leave[viz][1]) respf.push_back({leave[viz][0], leave[viz][1]});
else leave[cur][1]=leave[viz][0];
}
}
// printf("%d >> %d %d\n", cur, leave[cur][0], leave[cur][1]);
}
int main() {
scanf("%d", &n);
for(int i=1; i<=n-1; i++) {
int a, b; scanf("%d %d", &a, &b);
g[a].push_back(b); g[b].push_back(a);
}
for(int i=1; i<=n; i++) if(g[i].size()!=1) raiz=i;
dfs(raiz, raiz);
if(!leave[raiz][1]) leave[raiz][1]=raiz;
respf.push_back({leave[raiz][0], leave[raiz][1]});
printf("%d\n", respf.size());
for(auto cara : respf) printf("%d %d\n", cara.first, cara.second);
}
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... |