// "We create our own demons"
#include <bits/stdc++.h>
using namespace std;
#ifdef ONPC
#include "debug.h"
#else
#define dbg(...)
#endif
#define int long long
#define ll long long
#define ld long double
#define pi pair<int, int>
// vector
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(x) (*max_element(all(x)))
#define minel(x) (*min_element(all(x)))
#define maxpos(x) (max_element(all(x)) - x.begin())
#define minpos(x) (min_element(all(x)) - x.begin())
#define rev(x) (reverse(all(x)))
// bits
#define popcount(n) __builtin_popcountll(n)
#define clz(n) __builtin_clzll(n)
// math
#define sqr(n) ((n) * (n))
#define divup(a, b) (((a) + (b)-1) / (b))
// ostream
#define Fixed(a) cout << fixed << setprecision(12) << a;
template <class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool chmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; }
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_queue = priority_queue<T, vector<T>, less<T>>;
template <class T> using V = vector<T>;
using vi = V<int>;
using vd = V<ld>;
using vb = V<bool>;
using vpi = V<pi>;
using vvi = V<vi>;
using vvb = V<vb>;
const int mod = 1e9 + 7; // 998244353 1e9 + 7
const ll inf = (int)(1e18) + 7;
const int inf_s = 1e9 + 7;
const ld eps = 1e-9;
const int B = 32;
const int N = 1000 + 3;
const int logn = 20;
const int maxn = 2e5 + 7;
/////////////////////////solve/////////////////////////
vi g[maxn];
int dp[maxn][2];
vpi dp_par[maxn][2];
vi dp_swap[maxn][2];
int ans[maxn];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int v, u;
cin >> v >> u;
--v; --u;
g[v].push_back(u);
g[u].push_back(v);
}
for (int i = 0; i < n; ++i) {
ans[i] = i;
}
function<void(int, int)> dfs = [&](int v, int p) {
set <pi> s;
for (auto u : g[v]) {
if (u != p) {
dfs(u, v);
dp[v][0] += dp[u][1];
dp_par[v][0].push_back({u, 1});
s.insert({dp[u][0] - dp[u][1] + 2, u});
}
}
if (s.empty()) dp[v][1] = inf_s;
else {
int u = s.begin()->second;
dp_swap[v][1].push_back(u);
if (dp[u][0] <= dp[u][1]) {
dp[v][1] += dp[u][0] + 2;
dp_par[v][1].push_back({u, 0});
}
else {
dp[v][1] += dp[u][1] + 2;
dp_par[v][1].push_back({u, 1});
}
s.erase(s.begin());
for (auto& [x, u] : s) {
if (dp[u][0] + 2 < dp[u][1]) {
dp[v][1] += dp[u][0] + 2;
dp_par[v][1].push_back({u, 0});
dp_swap[v][1].push_back(u);
}
else {
dp[v][1] += dp[u][1];
dp_par[v][1].push_back({u, 1});
}
}
}
};
dfs(0, -1);
cout << dp[0][1] << " -1\n";
function<void(int, int, int)> dfs2 = [&](int v, int t, int p) {
for (auto& [u, t2] : dp_par[v][t]) {
dfs2(u, t2, v);
}
for (auto& u : dp_swap[v][t]) {
swap(ans[v], ans[u]);
}
};
dfs2(0, 1, -1);
for (int i = 0; i < n; ++i) {
cout << ans[i] + 1 << " ";
} cout << "\n";
for (int i = 0; i < n; ++i) {
cout << 1 << " ";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ONPC
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int t = 1;
//cin >> t;
for (int i = 1; i <= t; ++i) {
#ifdef ONPC
cerr << "===========" << i << "===========" << '\n';
#endif
solve();
}
#ifdef ONPC
cerr << endl << "Time " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
11 ms |
23896 KB |
Partially correct |
2 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
3 |
Partially correct |
9 ms |
23896 KB |
Partially correct |
4 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
5 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
6 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
7 |
Partially correct |
9 ms |
23812 KB |
Partially correct |
8 |
Partially correct |
11 ms |
23896 KB |
Partially correct |
9 |
Partially correct |
14 ms |
23824 KB |
Partially correct |
10 |
Partially correct |
15 ms |
23900 KB |
Partially correct |
11 |
Partially correct |
12 ms |
24156 KB |
Partially correct |
12 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
13 |
Partially correct |
9 ms |
23896 KB |
Partially correct |
14 |
Partially correct |
10 ms |
23884 KB |
Partially correct |
15 |
Partially correct |
12 ms |
23900 KB |
Partially correct |
16 |
Partially correct |
9 ms |
23756 KB |
Partially correct |
17 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
2 |
Partially correct |
10 ms |
23896 KB |
Partially correct |
3 |
Partially correct |
11 ms |
23900 KB |
Partially correct |
4 |
Partially correct |
12 ms |
23956 KB |
Partially correct |
5 |
Partially correct |
14 ms |
23956 KB |
Partially correct |
6 |
Partially correct |
10 ms |
24100 KB |
Partially correct |
7 |
Partially correct |
12 ms |
24152 KB |
Partially correct |
8 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
9 |
Partially correct |
10 ms |
23872 KB |
Partially correct |
10 |
Partially correct |
12 ms |
24156 KB |
Partially correct |
11 |
Partially correct |
11 ms |
23900 KB |
Partially correct |
12 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
13 |
Partially correct |
10 ms |
24332 KB |
Partially correct |
14 |
Partially correct |
12 ms |
23956 KB |
Partially correct |
15 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
16 |
Partially correct |
10 ms |
23956 KB |
Partially correct |
17 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
18 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
19 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
20 |
Partially correct |
9 ms |
24152 KB |
Partially correct |
21 |
Partially correct |
12 ms |
23900 KB |
Partially correct |
22 |
Partially correct |
9 ms |
24044 KB |
Partially correct |
23 |
Partially correct |
10 ms |
23896 KB |
Partially correct |
24 |
Partially correct |
13 ms |
23896 KB |
Partially correct |
25 |
Partially correct |
11 ms |
23800 KB |
Partially correct |
26 |
Partially correct |
10 ms |
24096 KB |
Partially correct |
27 |
Partially correct |
13 ms |
23900 KB |
Partially correct |
28 |
Partially correct |
9 ms |
23896 KB |
Partially correct |
29 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
30 |
Partially correct |
10 ms |
23896 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
11 ms |
23896 KB |
Partially correct |
2 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
3 |
Partially correct |
9 ms |
23896 KB |
Partially correct |
4 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
5 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
6 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
7 |
Partially correct |
9 ms |
23812 KB |
Partially correct |
8 |
Partially correct |
11 ms |
23896 KB |
Partially correct |
9 |
Partially correct |
14 ms |
23824 KB |
Partially correct |
10 |
Partially correct |
15 ms |
23900 KB |
Partially correct |
11 |
Partially correct |
12 ms |
24156 KB |
Partially correct |
12 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
13 |
Partially correct |
9 ms |
23896 KB |
Partially correct |
14 |
Partially correct |
10 ms |
23884 KB |
Partially correct |
15 |
Partially correct |
12 ms |
23900 KB |
Partially correct |
16 |
Partially correct |
9 ms |
23756 KB |
Partially correct |
17 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
18 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
19 |
Partially correct |
10 ms |
23896 KB |
Partially correct |
20 |
Partially correct |
11 ms |
23900 KB |
Partially correct |
21 |
Partially correct |
12 ms |
23956 KB |
Partially correct |
22 |
Partially correct |
14 ms |
23956 KB |
Partially correct |
23 |
Partially correct |
10 ms |
24100 KB |
Partially correct |
24 |
Partially correct |
12 ms |
24152 KB |
Partially correct |
25 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
26 |
Partially correct |
10 ms |
23872 KB |
Partially correct |
27 |
Partially correct |
12 ms |
24156 KB |
Partially correct |
28 |
Partially correct |
11 ms |
23900 KB |
Partially correct |
29 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
30 |
Partially correct |
10 ms |
24332 KB |
Partially correct |
31 |
Partially correct |
12 ms |
23956 KB |
Partially correct |
32 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
33 |
Partially correct |
10 ms |
23956 KB |
Partially correct |
34 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
35 |
Partially correct |
9 ms |
23900 KB |
Partially correct |
36 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
37 |
Partially correct |
9 ms |
24152 KB |
Partially correct |
38 |
Partially correct |
12 ms |
23900 KB |
Partially correct |
39 |
Partially correct |
9 ms |
24044 KB |
Partially correct |
40 |
Partially correct |
10 ms |
23896 KB |
Partially correct |
41 |
Partially correct |
13 ms |
23896 KB |
Partially correct |
42 |
Partially correct |
11 ms |
23800 KB |
Partially correct |
43 |
Partially correct |
10 ms |
24096 KB |
Partially correct |
44 |
Partially correct |
13 ms |
23900 KB |
Partially correct |
45 |
Partially correct |
9 ms |
23896 KB |
Partially correct |
46 |
Partially correct |
10 ms |
23900 KB |
Partially correct |
47 |
Partially correct |
10 ms |
23896 KB |
Partially correct |
48 |
Partially correct |
69 ms |
37104 KB |
Partially correct |
49 |
Partially correct |
77 ms |
38484 KB |
Partially correct |
50 |
Partially correct |
80 ms |
38480 KB |
Partially correct |
51 |
Partially correct |
58 ms |
35156 KB |
Partially correct |
52 |
Partially correct |
88 ms |
38228 KB |
Partially correct |
53 |
Partially correct |
88 ms |
36868 KB |
Partially correct |
54 |
Partially correct |
54 ms |
41412 KB |
Partially correct |
55 |
Partially correct |
131 ms |
60280 KB |
Partially correct |
56 |
Partially correct |
109 ms |
48724 KB |
Partially correct |
57 |
Partially correct |
119 ms |
45396 KB |
Partially correct |
58 |
Partially correct |
113 ms |
42368 KB |
Partially correct |
59 |
Partially correct |
87 ms |
38736 KB |
Partially correct |
60 |
Partially correct |
88 ms |
43476 KB |
Partially correct |
61 |
Partially correct |
78 ms |
39288 KB |
Partially correct |
62 |
Partially correct |
71 ms |
36860 KB |
Partially correct |
63 |
Partially correct |
67 ms |
36344 KB |
Partially correct |
64 |
Partially correct |
78 ms |
37972 KB |
Partially correct |
65 |
Partially correct |
78 ms |
38140 KB |
Partially correct |
66 |
Partially correct |
76 ms |
36632 KB |
Partially correct |
67 |
Partially correct |
74 ms |
35276 KB |
Partially correct |
68 |
Partially correct |
59 ms |
35832 KB |
Partially correct |
69 |
Partially correct |
70 ms |
38020 KB |
Partially correct |
70 |
Partially correct |
90 ms |
37156 KB |
Partially correct |
71 |
Partially correct |
48 ms |
33360 KB |
Partially correct |
72 |
Partially correct |
58 ms |
34840 KB |
Partially correct |
73 |
Partially correct |
73 ms |
38228 KB |
Partially correct |
74 |
Partially correct |
96 ms |
36688 KB |
Partially correct |
75 |
Partially correct |
100 ms |
38272 KB |
Partially correct |
76 |
Partially correct |
77 ms |
38408 KB |
Partially correct |
77 |
Partially correct |
74 ms |
38992 KB |
Partially correct |
78 |
Partially correct |
53 ms |
33188 KB |
Partially correct |
79 |
Partially correct |
52 ms |
34128 KB |
Partially correct |
80 |
Partially correct |
86 ms |
38316 KB |
Partially correct |
81 |
Partially correct |
61 ms |
39504 KB |
Partially correct |
82 |
Partially correct |
76 ms |
36100 KB |
Partially correct |