Submission #257797

#TimeUsernameProblemLanguageResultExecution timeMemory
257797VEGAnnVillage (BOI20_village)C++14
0 / 100
3 ms2688 KiB
#include <bits/stdc++.h> #define sz(x) ((int)x.size()) #define PB push_back #define i2 array<int,2> using namespace std; typedef long long ll; const int N = 100100; const int oo = 2e9; set<i2> setik, oth; vector<int> g[N], vc; int n, siz[N], cen, who_kto[N], mrk[N]; ll ans, ANS; void dfs(int v, int p){ for (int u : g[v]){ if (u == p) continue; dfs(u, v); if (mrk[u] < 0){ ANS += 2; if (mrk[v] < 0){ mrk[v] = u; mrk[u] = v; } else { mrk[u] = mrk[v]; mrk[v] = u; } } } } void build_sz(int v, int p){ siz[v] = 1; for (int u : g[v]){ if (u == p) continue; build_sz(u, v); siz[v] += siz[u]; ans += min(siz[u], n - siz[u]); } } int search(int v, int p, int SZ){ for (int u : g[v]) if (u != p && siz[u] > SZ / 2) return search(u, v, SZ); return v; } void add(int v, int p){ vc.PB(v); for (int u : g[v]){ if (u == p) continue; add(u, v); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); #ifdef _LOCAL freopen("in.txt","r",stdin); #endif // _LOCAL cin >> n; for (int i = 1; i < n; i++){ int x, y; cin >> x >> y; x--; y--; g[x].PB(y); g[y].PB(x); } fill(mrk, mrk + n, -1); dfs(0, -1); if (mrk[0] < 0) { ans += 2; mrk[0] = mrk[g[0][0]]; mrk[g[0][0]] = 0; } build_sz(0, -1); cout << ans * 2 << " " << ANS << '\n'; for (int i = 0; i < n; i++) who_kto[mrk[i]] = i; for (int i = 0; i < n; i++) cout << who_kto[i] + 1 << " "; cout << '\n'; cen = search(0, -1, siz[0]); for (int u : g[cen]) add(u, cen); vc.PB(cen); for (int i = 0; i < n; i++) who_kto[vc[i]] = vc[(i + n / 2) % n]; for (int i = 0; i < n; i++) cout << who_kto[i] + 1 << " "; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...