#include "bits/stdc++.h"
using namespace std;
using ll = long long;
template<typename T>
using V = vector<T>;
using vi = V<ll>;
using vvi = V<vi>;
using pi = pair<ll,ll>;
using vpi = V<pi>;
#define F0R(i, n) for(ll i = 0; i < n;i++)
#define FOR(i,a, n) for(ll i = a; i < n;i++)
#define pb push_back
vvi g;
vi to;
vi dist;
vi sum;
V<bool> vis;
vi org;
ll n;
// Dist, Node
V<queue<pi>> pqs;
void calcOrg(int x, int p, int orgP) {
org[x] = orgP;
for (auto y : g[x]) {
if (y == p) continue;
calcOrg(y,x,orgP == -1 ? x : orgP);
}
}
void calc(ll x, ll p, ll d = 0) {
dist[x] = d;
for (auto y : g[x]) {
if (y == p) continue;
calc(y,x, d + 1);
}
}
void sumDfs(int x, int p) {
sum[x] = 1;
for (auto y : g[x]) {
if (y == p) continue;
sumDfs(y,x);
sum[x] += sum[y];
}
}
int findCentroid(int x, int p) {
for (auto v : g[x]) {
if (v == p) continue;
if (sum[v] * 2 > n)
return findCentroid(v,x);
}
return x;
}
void genPQS(int x, int p) {
queue<tuple<int,int,int>> nodes;
nodes.emplace(x, 1, p);
pqs[x].emplace(x, 1);
while (!nodes.empty()) {
auto [cur, dist, par] = nodes.front(); nodes.pop();
for (auto y : g[cur]) {
if (y == par) continue;
nodes.emplace(y, dist + 1, cur);
pqs[x].emplace(y, dist + 1);
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
g.resize(n + 1);
to.resize(n + 1);
vis.resize(n + 1);
dist.resize(n + 1);
org.resize(n + 1);
sum.resize(n + 1);
pqs.resize(n + 1);
F0R(i, n - 1) {
ll a, b; cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
FOR(i, 1, n + 1) sum[i]++;
sumDfs(1,-1);
int cen = findCentroid(1,-1);
sumDfs(cen,-1);
int root = cen;
ll sumV = 0;
int last = -1;
for (auto x : g[cen]) {
genPQS(x, cen);
}
priority_queue<pi> main;
for (auto x : g[cen]) {
main.emplace(sum[x], x);
}
while (true) {
bool done = false;
if (main.empty()) {
main.emplace(sum[last], last);
done = true;
}
auto [am, meow] = main.top(); main.pop();
if (last != -1 && !done) main.emplace(sum[last], last);
last = meow;
sum[last]--;
if (pqs[meow].empty()) break;
auto [sel, distToCen] = pqs[meow].front(); pqs[meow].pop();
to[root] = sel;
root = sel;
sumV += distToCen * 2;
}
to[root] = cen;
cout << sumV << " " << sumV << "\n";
FOR(i, 1, n + 1) {
cout << to[i] << " ";
}
cout << "\n";
FOR(i, 1, n + 1) {
cout << to[i] << " ";
}
return 0;
}
# |
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 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Partially correct |
1 ms |
336 KB |
Partially correct |
5 |
Partially correct |
1 ms |
380 KB |
Partially correct |
6 |
Partially correct |
1 ms |
504 KB |
Partially correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Partially correct |
1 ms |
336 KB |
Partially correct |
9 |
Partially correct |
1 ms |
336 KB |
Partially correct |
10 |
Partially correct |
1 ms |
336 KB |
Partially correct |
11 |
Partially correct |
1 ms |
336 KB |
Partially correct |
12 |
Partially correct |
1 ms |
336 KB |
Partially correct |
13 |
Partially correct |
1 ms |
336 KB |
Partially correct |
14 |
Correct |
1 ms |
508 KB |
Output is correct |
15 |
Partially correct |
1 ms |
336 KB |
Partially correct |
16 |
Partially correct |
1 ms |
336 KB |
Partially correct |
17 |
Partially correct |
1 ms |
336 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
592 KB |
Partially correct |
2 |
Partially correct |
1 ms |
848 KB |
Partially correct |
3 |
Partially correct |
1 ms |
848 KB |
Partially correct |
4 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
5 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
6 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
7 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
8 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
9 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
10 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
11 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
12 |
Correct |
2 ms |
1104 KB |
Output is correct |
13 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
14 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
15 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
16 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
17 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
18 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
19 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
20 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
21 |
Partially correct |
1 ms |
1272 KB |
Partially correct |
22 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
23 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
24 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
25 |
Partially correct |
1 ms |
848 KB |
Partially correct |
26 |
Partially correct |
1 ms |
1272 KB |
Partially correct |
27 |
Partially correct |
1 ms |
848 KB |
Partially correct |
28 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
29 |
Partially correct |
2 ms |
1272 KB |
Partially correct |
30 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
# |
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 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Partially correct |
1 ms |
336 KB |
Partially correct |
5 |
Partially correct |
1 ms |
380 KB |
Partially correct |
6 |
Partially correct |
1 ms |
504 KB |
Partially correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Partially correct |
1 ms |
336 KB |
Partially correct |
9 |
Partially correct |
1 ms |
336 KB |
Partially correct |
10 |
Partially correct |
1 ms |
336 KB |
Partially correct |
11 |
Partially correct |
1 ms |
336 KB |
Partially correct |
12 |
Partially correct |
1 ms |
336 KB |
Partially correct |
13 |
Partially correct |
1 ms |
336 KB |
Partially correct |
14 |
Correct |
1 ms |
508 KB |
Output is correct |
15 |
Partially correct |
1 ms |
336 KB |
Partially correct |
16 |
Partially correct |
1 ms |
336 KB |
Partially correct |
17 |
Partially correct |
1 ms |
336 KB |
Partially correct |
18 |
Partially correct |
1 ms |
592 KB |
Partially correct |
19 |
Partially correct |
1 ms |
848 KB |
Partially correct |
20 |
Partially correct |
1 ms |
848 KB |
Partially correct |
21 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
22 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
23 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
24 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
25 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
26 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
27 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
28 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
29 |
Correct |
2 ms |
1104 KB |
Output is correct |
30 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
31 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
32 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
33 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
34 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
35 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
36 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
37 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
38 |
Partially correct |
1 ms |
1272 KB |
Partially correct |
39 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
40 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
41 |
Partially correct |
1 ms |
1104 KB |
Partially correct |
42 |
Partially correct |
1 ms |
848 KB |
Partially correct |
43 |
Partially correct |
1 ms |
1272 KB |
Partially correct |
44 |
Partially correct |
1 ms |
848 KB |
Partially correct |
45 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
46 |
Partially correct |
2 ms |
1272 KB |
Partially correct |
47 |
Partially correct |
2 ms |
1104 KB |
Partially correct |
48 |
Partially correct |
84 ms |
72528 KB |
Partially correct |
49 |
Partially correct |
93 ms |
79888 KB |
Partially correct |
50 |
Partially correct |
93 ms |
79944 KB |
Partially correct |
51 |
Partially correct |
75 ms |
62280 KB |
Partially correct |
52 |
Partially correct |
90 ms |
78816 KB |
Partially correct |
53 |
Partially correct |
84 ms |
70988 KB |
Partially correct |
54 |
Partially correct |
54 ms |
42576 KB |
Partially correct |
55 |
Partially correct |
104 ms |
85140 KB |
Partially correct |
56 |
Partially correct |
105 ms |
82504 KB |
Partially correct |
57 |
Partially correct |
99 ms |
80632 KB |
Partially correct |
58 |
Partially correct |
94 ms |
80712 KB |
Partially correct |
59 |
Partially correct |
100 ms |
79944 KB |
Partially correct |
60 |
Correct |
100 ms |
80444 KB |
Output is correct |
61 |
Partially correct |
89 ms |
80192 KB |
Partially correct |
62 |
Partially correct |
83 ms |
80200 KB |
Partially correct |
63 |
Partially correct |
76 ms |
75476 KB |
Partially correct |
64 |
Partially correct |
82 ms |
80200 KB |
Partially correct |
65 |
Partially correct |
84 ms |
80464 KB |
Partially correct |
66 |
Partially correct |
79 ms |
75336 KB |
Partially correct |
67 |
Partially correct |
73 ms |
59588 KB |
Partially correct |
68 |
Partially correct |
76 ms |
68100 KB |
Partially correct |
69 |
Partially correct |
89 ms |
80712 KB |
Partially correct |
70 |
Partially correct |
80 ms |
75592 KB |
Partially correct |
71 |
Partially correct |
59 ms |
56392 KB |
Partially correct |
72 |
Partially correct |
68 ms |
63576 KB |
Partially correct |
73 |
Partially correct |
86 ms |
80724 KB |
Partially correct |
74 |
Partially correct |
76 ms |
73544 KB |
Partially correct |
75 |
Partially correct |
92 ms |
80456 KB |
Partially correct |
76 |
Partially correct |
92 ms |
79944 KB |
Partially correct |
77 |
Partially correct |
86 ms |
76112 KB |
Partially correct |
78 |
Partially correct |
61 ms |
53576 KB |
Partially correct |
79 |
Partially correct |
70 ms |
62536 KB |
Partially correct |
80 |
Partially correct |
95 ms |
80456 KB |
Partially correct |
81 |
Partially correct |
86 ms |
75856 KB |
Partially correct |
82 |
Partially correct |
84 ms |
79432 KB |
Partially correct |