Submission #500415

#TimeUsernameProblemLanguageResultExecution timeMemory
500415zhougzVillage (BOI20_village)C++17
100 / 100
110 ms28032 KiB
#include <bits/stdc++.h> using namespace std; #define int long long int n; const int MAXN = 100'050; vector<vector<int>> v(MAXN, vector<int>()); pair<int, vector<int>> slv1(int n, vector<vector<int>> v) { int res = 0; vector<int> arr(n); // arr[i] denotes guy at place where i was iota(arr.begin(), arr.end(), 0); function<void(int, int)> dfs = [&](int x, int p) -> void { for (auto z : v[x]) { if (z != p) { dfs(z, x); } } if (arr[x] == x) { if (x != 0) { swap(arr[x], arr[p]); } else { swap(arr[0], arr[v[0][0]]); } res += 2; } }; dfs(0, -1); vector<int> ans(n); for (int i = 0; i < n; i++) { ans[arr[i]] = i; } return make_pair(res, ans); } pair<int, vector<int>> slv2(int n, vector<vector<int>> v) { int mx = 0, cnt = 0, centroid = -1; vector<int> sz(n), pre(n); function<void(int, int)> dfs = [&](int x, int p) -> void { pre[cnt++] = x; sz[x] = 1; for (auto z : v[x]) { if (z != p) { dfs(z, x); sz[x] += sz[z]; mx += min(sz[z], n - sz[z]); } } if (sz[x] > n / 2 && centroid == -1) { centroid = x; } }; dfs(0, -1); vector<int> ans(n); for (int i = 0; i < n; i++) { ans[pre[i]] = pre[(i + n / 2) % n]; } return make_pair(2 * mx, ans); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0, a, b; i < n - 1; i++) { cin >> a >> b; a--; b--; v[a].push_back(b); v[b].push_back(a); } int res1, res2; vector<int> arr1, arr2; tie(res1, arr1) = slv1(n, v); tie(res2, arr2) = slv2(n, v); cout << res1 << ' ' << res2 << '\n'; for (auto x : arr1) { cout << x + 1 << ' '; } cout << '\n'; for (auto x : arr2) { cout << x + 1 << ' '; } cout << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...