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;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int x; cin >> x; x--;
int y; cin >> y; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int> amin(n);
iota(amin.begin(), amin.end(), 0);
int ansmin = 0;
auto dfs = [&](auto dfs, int x, int p) -> bool {
vector<int> b;
for (int y : adj[x]) {
if (y != p) {
if (dfs(dfs, y, x)) {
b.push_back(y);
}
}
}
if (b.empty()) {
return 1;
}
ansmin += b.size() * 2;
if (b.size() % 2 == 1) {
swap(amin[x], amin[b[0]]);
for (int i = 1; i < b.size(); i += 2) {
swap(amin[b[i]], amin[b[i + 1]]);
}
} else {
swap(amin[x], amin[b[0]]);
swap(amin[x], amin[b[1]]);
for (int i = 2; i < b.size(); i += 2) {
swap(amin[b[i]], amin[b[i + 1]]);
}
}
return 0;
};
if (dfs(dfs, 0, -1)) {
swap(amin[0], amin[adj[0][0]]);
ansmin += 2;
}
set<int> s;
for (int i = 0; i < n; i++) {
s.insert(i);
}
auto bfs = [&](int src) {
vector<int> dis(n, -1);
queue<int> q;
dis[src] = 0;
q.push(src);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int y : adj[x]) {
if (dis[y] == -1 && s.count(y)) {
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
return dis;
};
vector<int> amax(n);
iota(amax.begin(), amax.end(), 0);
int64_t ansmax = 0;
if (n <= 1000) {
while (s.size() > 1) {
int t = *s.begin();
auto dis = bfs(t);
int u = max_element(dis.begin(), dis.end()) - dis.begin();
dis = bfs(u);
int v = max_element(dis.begin(), dis.end()) - dis.begin();
swap(amax[u], amax[v]);
ansmax += 2 * dis[v];
s.erase(u);
s.erase(v);
}
if (s.size() == 1) {
int x = *s.begin();
swap(amax[x], amax[adj[x][0]]);
}
}
cout << ansmin << " " << ansmax << "\n";
for (int i = 0; i < n; i++) {
cout << amin[i] + 1 << " \n"[i == n - 1];
}
for (int i = 0; i < n; i++) {
cout << amax[i] + 1 << " \n"[i == n - 1];
}
return 6/22;
}
Compilation message (stderr)
Village.cpp: In lambda function:
Village.cpp:33:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int i = 1; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp:39:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 2; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp: In instantiation of 'main()::<lambda(auto:23, int, int)> [with auto:23 = main()::<lambda(auto:23, int, int)>]':
Village.cpp:45:23: required from here
Village.cpp:33:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int i = 1; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp:39:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 2; i < b.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... |