Submission #932512

#TimeUsernameProblemLanguageResultExecution timeMemory
932512a_l_i_r_e_z_aVillage (BOI20_village)C++17
100 / 100
69 ms17352 KiB
// In the name of God // Hope is last to die #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define pb push_back // #define int long long #define S second #define F first #define mp make_pair #define smax(x, y) (x) = max((x), (y)) #define smin(x, y) (x) = min((x), (y)) #define all(x) (x).begin(), (x).end() #define len(x) ((int)(x).size()) const int maxn = 1e5 + 5; const int inf = 1e9 + 7; int n, ans[maxn], res, timer, sz[maxn], in[maxn]; vector<int> adj[maxn]; ll ans__; vector<int> vec; void dfs(int v = 1, int p = 0){ ans[v] = v; for(auto u: adj[v]) if(u != p) dfs(u, v); if(ans[v] == v){ res += 2; if(p) swap(ans[p], ans[v]); else swap(ans[v], ans[adj[v][0]]); } } void get_sz(int v, int p = -1){ sz[v] = 1; for(auto u: adj[v]){ if(u != p){ get_sz(u, v); sz[v] += sz[u]; } } } int find_centroid(int v, int p = -1){ for(auto u: adj[v]) if(u != p && sz[u] * 2 > n) return find_centroid(u, v); return v; } void dfs__(int v, int h = 0, int p = -1){ in[v] = timer++; vec.pb(v); ans__ += h; for(auto u: adj[v]) if(u != p) dfs__(u, h + 1, v); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 0; i < n - 1; i++){ int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } dfs(); get_sz(1); int v = find_centroid(1); dfs__(v); cout << res << ' '; cout << ans__ * 2 << '\n'; for(int i = 1; i <= n; i++) cout << ans[i] << ' '; cout << '\n'; for(int i = 1; i <= n; i++) cout << vec[(in[i] + n / 2) % n] << ' '; cout << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...