This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |