이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//BOI 2020
#include<bits/stdc++.h>
using namespace std;
int dfsbig(int v, int p, vector<vector<int> >& adi, vector<int>& pre, int n, int& ans){
pre.push_back(v);
int s=1;
for(auto u: adi[v]){
if(u==p) continue;
s+=dfsbig(u, v, adi, pre, n, ans);
}
ans += 2*min(s, n-s);
return s;
}
bool dfssmall(int v, int p, vector<vector<int> >& adi, vector<int>& go, int& ans, int n){
vector<int> unmatched;
for(auto u: adi[v]){
if(u==p) continue;
if(dfssmall(u, v, adi, go, ans, n)){
unmatched.push_back(u);
}
}
if(unmatched.empty()){
ans+=2;
return true;
}
unmatched.push_back(v);
int s=unmatched.size();
for(int i=0; i<s; i++){
go[unmatched[i]] = unmatched[(i+1)%s];
}
return false;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int> > adi(n);
for(int i=0; i<n-1; i++){
int a, b;
cin >> a >> b;
a--; b--;
adi[a].push_back(b);
adi[b].push_back(a);
}
int leaf=-1;
for(int i=0; i<n; i++){
if(adi[i].size()==1) leaf=i;
}
int smallest=0;
vector<int> gosmall(n);
dfssmall(adi[leaf][0], -1, adi, gosmall, smallest, n);
vector<int> pre;
int biggest=0;
dfsbig(0, -1, adi, pre, n, biggest);
vector<int> gobig(n);
for(int i=0; i<n; i++){
gobig[pre[i]] = pre[(i+n/2)%n];
}
cout << smallest << " " << biggest << "\n";
for(int i=0; i<n; i++){
cout << gosmall[i]+1 << " ";
}
cout << "\n";
for(int i=0; i<n; i++){
cout << gobig[i]+1 << " ";
}
cout << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |