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;
#ifdef MIKU
const string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << '[' << #x << "] : ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
const int MXN = 100005;
int n;
vector<int> edge[MXN];
namespace NSA {
int ans, va[MXN], sz[MXN];
int bind[MXN];
void DFS(int id, int par) {
sz[id] = 1;
vector<int> odd;
for (auto &i : edge[id]) {
if (i == par) continue;
DFS(i, id);
sz[id] += sz[i];
if (sz[i] & 1) odd.push_back(i);
}
odd.push_back(id);
for (int i = 1; i < odd.size(); i += 2) {
bind[odd[i - 1]] = odd[i];
bind[odd[i]] = odd[i - 1];
ans += (odd[i] == id ? 2 : 4);
}
}
void DO() {
int rt = [&]() -> int {
FOR(i, 1, n + 1) if (edge[i].size() == 1) return i;
return 0;
}();
DFS(rt, 0);
if (sz[rt] & 1) {
bind[rt] = rt;
swap(bind[rt], bind[edge[rt][0]]);
ans += 2;
}
copy(bind + 1, bind + n + 1, va + 1);
}
}
namespace NSB {
int ans, va[MXN];
int sz[MXN];
bitset<MXN> ban;
int GET_ANS(int id, int par) {
sz[id] = 1;
int ans = 0;
for (auto &i : edge[id]) {
if (i == par) continue;
ans += GET_ANS(i, id);
sz[id] += sz[i];
}
return (ans + min(n - sz[id], sz[id]) * 2);
}
void GET_SZ(int id, int par) {
sz[id] = 1;
for (auto &i : edge[id]) {
if (i == par) continue;
if (ban[i]) continue;
GET_SZ(i, id);
sz[id] += sz[i];
}
}
int GET_CD(int id, int par, int N) {
for (auto &i : edge[id]) {
if (i == par) continue;
if (ban[i]) continue;
if (sz[i] > N / 2) return GET_CD(i, id, N);
}
return id;
}
vector<int> CD(int id) {
GET_SZ(id, 0);
if (sz[id] == 1) return vector<int>(1, id);
vector<int> w(sz[id]), wid;
for (int i = 0; i < sz[id]; i += 2) wid.push_back(i);
for (int i = 1; i < sz[id]; i += 2) wid.push_back(i);
int cd = GET_CD(id, 0, sz[id]);
vector<vector<int>> v;
ban[cd] = true;
for (auto &i : edge[cd]) {
if (ban[i]) continue;
v.push_back(CD(i));
}
sort(v.begin(), v.end(), [&](vector<int> &a, vector<int> &b) -> bool {
return a.size() > b.size();
});
v.back().push_back(cd);
int C = 0;
for (auto &i : v) for (auto &j : i) w[wid[C++]] = j;
// debug(id);
// for (auto &i : w) cout << i << ' ';
// cout << endl;
return w;
}
void DO() {
ans = GET_ANS(1, 0);
vector<int> v = CD(1);
// cout << dbmc;
// for (auto &i : v) cout << i << ' ';
// cout << dbrs << endl;
v.push_back(v[0]);
FOR(i, 1, n + 1) va[v[i - 1]] = v[i];
}
}
void miku() {
int x, y;
cin >> n;
FOR(i, 1, n) {
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
NSA::DO();
NSB::DO();
cout << NSA::ans << ' ' << NSB::ans << '\n';
FOR(i, 1, n + 1) cout << NSA::va[i] << ' ';
cout << '\n';
FOR(i, 1, n + 1) cout << NSB::va[i] << ' ';
cout << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(iostream::failbit);
miku();
return 0;
}
Compilation message (stderr)
Village.cpp: In function 'void NSA::DFS(long long int, long long int)':
Village.cpp:39:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 1; i < odd.size(); i += 2) {
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |