#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define lb lower_bound
#define ub upper_bound
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
void solve() {
int n;
cin >> n;
vector <int> graph[n];
for (int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
graph[a].pb(b);
graph[b].pb(a);
}
int cost[2];
cost[0] = cost[1] = 0;
vector <vector <int>> rear(2, vector <int> (n));
//~ int df = 0, vf = 0;
//~ function <void(int, int, int)> find_furthest = [&](int v, int p, int dist) {
//~ if (dist > df) {
//~ df = dist;
//~ vf = v;
//~ }
//~ for (auto to : graph[v]) {
//~ if (to == p) continue;
//~ find_furthest(to, v, dist+1);
//~ }
//~ };
//~ int d1, d2;
//~ find_furthest(0, 0, 0);
//~ d1 = vf;
//~ df = vf = 0;
//~ find_furthest(d1, d1, 0);
//~ d2 = vf;
//~ cost[1] = df;
//~ rear[1][d1] = d2;
//~ rear[1][d2] = d1;
//~ vector <int> left;
//~ for (int i = 0; i < n; i++) {
//~ if (i == d1 || i == d2) {
//~ continue;
//~ }
//~ left.pb(i);
//~ }
//~ for (int i = 0; i < n-2; i++) {
//~ rear[0][left[i]] = left[(i+1)%(n-2)];
//~ }
function <int(int, int)> find_smallest = [&](int v, int p) {
bool available = 1;
int child = -1;
for (auto to : graph[v]) {
if (to == p) continue;
if (find_smallest(to, v)) {
if (child == -1) {
child = to;
}
else {
rear[0][child] = to;
rear[0][to] = child;
child = -1;
cost[0] += 2;
}
}
}
if (child != -1) {
available = 0;
rear[0][child] = v;
rear[0][v] = child;
child = -1;
cost[0] += 2;
}
if (!v && available) {
available = 0;
cost[0] += 2;
int A, B;
for (auto to : graph[v]) {
if (to == p) continue;
A = to; B = rear[0][to];
break;
}
rear[0][v] = B;
rear[0][B] = A;
rear[0][A] = v;
}
return available;
};
find_smallest(0, 0);
cout << cost[0] << ' ' << cost[1] << endl;
for (int i = 0; i < 2; i++) {
for (auto to : rear[i]) cout << to+1 << ' ';
cout << endl;
}
}
int main() {
do_not_disturb
int t = 1;
//~ cin >> t;
while (t--) {
solve();
}
return 0;
}
Compilation message
Village.cpp: In static member function 'static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes&& ...) [with _Res = int; _Functor = solve()::<lambda(int, int)>; _ArgTypes = {int, int}]':
Village.cpp:101:22: warning: 'A' may be used uninitialized in this function [-Wmaybe-uninitialized]
101 | rear[0][A] = v;
| ^
Village.cpp:93:17: note: 'A' was declared here
93 | int A, B;
| ^
Village.cpp:99:24: warning: 'B' may be used uninitialized in this function [-Wmaybe-uninitialized]
99 | rear[0][v] = B;
Village.cpp:93:20: note: 'B' was declared here
93 | int A, B;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
212 KB |
Partially correct |
2 |
Partially correct |
0 ms |
212 KB |
Partially correct |
3 |
Partially correct |
0 ms |
212 KB |
Partially correct |
4 |
Partially correct |
0 ms |
212 KB |
Partially correct |
5 |
Partially correct |
0 ms |
212 KB |
Partially correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
212 KB |
Partially correct |
2 |
Partially correct |
0 ms |
212 KB |
Partially correct |
3 |
Partially correct |
0 ms |
212 KB |
Partially correct |
4 |
Partially correct |
0 ms |
212 KB |
Partially correct |
5 |
Partially correct |
0 ms |
212 KB |
Partially correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |