Submission #781116

#TimeUsernameProblemLanguageResultExecution timeMemory
781116mnbvcxz123Village (BOI20_village)C++17
0 / 100
1 ms340 KiB
#include<bits/stdc++.h> using namespace std; using ll=long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin>>n; vector<vector<int>>g(n); for(int i=1,u,v;i<n;++i){ cin>>u>>v; --u,--v; g[u].push_back(v); g[v].push_back(u); } vector<int>ord,sz(n); ll cnt=0; function<void(int,int)>dfs=[&](int u, int p){ sz[u]=1; ord.push_back(u); for(const int&i:g[u]) if(i!=p){ dfs(i,u); sz[u]+=sz[i]; cnt+=min(sz[i],n-sz[i]); } }; dfs(0,0); vector<int>ans(n); for(int i=0;i<n;++i){ int u=ord[i]; int v=ord[(i+n/2)%n]; ans[v]=u; } cout<<2*cnt<<'\n'; for(int i=0;i<n;++i)cout<<ans[i]+1<<' '; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...