#include "bits/stdc++.h"
using namespace std;
template<typename T>
using V = vector<T>;
using vi = V<int>;
using vvi = V<vi>;
using pi = pair<int,int>;
using vpi = V<pi>;
#define F0R(i, n) for(int i = 0; i < n;i++)
#define FOR(i,a, n) for(int i = a; i < n;i++)
#define pb push_back
V<bool> state;
int sum;
vi to;
vvi g;
void dfs(int x, int p) {
vi child;
for (auto v : g[x]) {
if (p == v) continue;
dfs(v, x);
if (state[v]) continue;
child.pb(v);
}
int stop = 25;
for (int i = 0; i < (int)child.size() / 2; ++i) {
to[child[i]] = child[i + 1];
to[child[i + 1]] = child[i];
sum += 4;
}
if (child.size() % 2 == 1) {
to[child[child.size() - 1]] = x;
to[x] = child[child.size() - 1];
sum += 2;
state[x] = true;
}
if ((child.size() > 1 || p == -1) && !state[x]) {
if (p == -1 && child.size() < 2) sum += 2;
int node = g[x][0];
if (node == p) node = g[x][1];
to[x] = node;
to[to[node]] = x;
state[x] = true;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
g.resize(n + 1);
state.resize(n + 1);
to.resize(n + 1);
F0R(i, n - 1) {
int a, b; cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
dfs(n,-1);
cout << sum << " 6\n";
FOR(i, 1, n + 1)
cout << to[i] << " ";
cout << "\n";
FOR(i, 1, n + 1)
cout << to[i] << " ";
return 0;
}
Compilation message
Village.cpp: In function 'void dfs(int, int)':
Village.cpp:29:9: warning: unused variable 'stop' [-Wunused-variable]
29 | int stop = 25;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
336 KB |
Partially correct |
2 |
Partially correct |
1 ms |
336 KB |
Partially correct |
3 |
Partially correct |
1 ms |
336 KB |
Partially correct |
4 |
Partially correct |
1 ms |
336 KB |
Partially correct |
5 |
Partially correct |
1 ms |
336 KB |
Partially correct |
6 |
Partially correct |
1 ms |
336 KB |
Partially correct |
7 |
Incorrect |
1 ms |
336 KB |
Integer parameter [name=vi] equals to 0, violates the range [1, 7] |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Integer parameter [name=vi] equals to 0, violates the range [1, 256] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
336 KB |
Partially correct |
2 |
Partially correct |
1 ms |
336 KB |
Partially correct |
3 |
Partially correct |
1 ms |
336 KB |
Partially correct |
4 |
Partially correct |
1 ms |
336 KB |
Partially correct |
5 |
Partially correct |
1 ms |
336 KB |
Partially correct |
6 |
Partially correct |
1 ms |
336 KB |
Partially correct |
7 |
Incorrect |
1 ms |
336 KB |
Integer parameter [name=vi] equals to 0, violates the range [1, 7] |
8 |
Halted |
0 ms |
0 KB |
- |