#include <iostream>
#include <algorithm>
#include <vector>
#include <assert.h>
#define vi vector<int>
#define pub push_back
using namespace std;
int n;
vi adj[100001];
int res[100001], dp[100001][2];
int mtcW[100001];
int res2[100001], sub[100001];
long long k = 0;
vector<vi> ord;
void dfs(int u, int p)
{
dp[u][0] = dp[u][1] = 0;
int ret = 0;
for (auto v : adj[u])
{
if (v != p)
{
dfs(v, u);
ret += dp[v][1];
}
}
dp[u][0] = ret;
for (auto v : adj[u])
{
if (v != p)
{
assert(dp[v][1] >= dp[v][0]);
dp[u][1] = max(dp[u][1], 1 + ret - dp[v][1] + dp[v][0]);
}
}
}
void bktrk(int u, int p, int s)
{
if (dp[u][s] == 0 && res[u] == -1)
{
res[u] = res[p];
res[p] = u;
}
if (s)
{
int ret = 0, spc = -1;
for (auto v : adj[u])
{
if (v != p) {ret += dp[v][1];}
}
for (auto v : adj[u])
{
if (v != p)
{
assert(dp[v][1] >= dp[v][0]);
if (1 + ret - dp[v][1] + dp[v][0] == dp[u][1])
{
spc = v;
res[spc] = u;
res[u] = spc;
break;
}
}
}
for (auto v : adj[u])
{
if (v != p) bktrk(v, u, (spc != v));
}
}
else
{
for (auto v : adj[u])
{
if (v != p) {bktrk(v, u, 1);}
}
}
}
void computeSub(int u, int p)
{
sub[u] = 1;
for (auto v : adj[u])
{
if (v != p)
{
computeSub(v, u);
sub[u] += sub[v];
}
}
}
int findCentroid(int u, int p, int o)
{
for (auto v : adj[u])
{
if (v != p && sub[v] > sub[o] / 2) return findCentroid(v, u, o);
}
return u;
}
void dfs2(int u, int p)
{
sub[u] = 1;
if (p == -1) ord.push_back(vi());
ord.back().push_back(u);
for (auto v : adj[u])
{
if (p == -1) ord.push_back(vi());
if (v != p)
{
dfs2(v, u);
sub[u] += sub[v];
k += 2 * sub[v];
}
}
}
bool comp(const vi &a, const vi &b) {return a.size() < b.size();}
int main()
{
cin >> n;
for (int i = 0; i < n; i++) res[i] = -1;
for (int i = 1; i < n; i++)
{
int u, v; cin >> u >> v;
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
dfs(0, -1);
bktrk(0, -1, 1);
computeSub(0, -1);
int c = findCentroid(0, -1, 0);
dfs2(c, -1);
sort(ord.begin(), ord.end(), comp);
int j = ord.size() - 1, p = 0;
for (int i = 0; i < ord.size(); i++)
{
for (int i2 = 0; i2 < ord[i].size(); i2++)
{
res2[ord[i][i2]] = ord[j][p++];
if (p == ord[j].size()) {j = (j + 1) % ord.size(); p = 0;}
}
}
cout << 2 * (dp[0][1] + n - 2 * dp[0][1]) << " " << k << "\n";
for (int i = 0; i < n; i++)
{
cout << res[i] + 1;
if (i + 1 < n) cout << " ";
}
cout << "\n";
for (int i = 0; i < n; i++)
{
cout << res2[i] + 1;
if (i + 1 < n) cout << " ";
}
cout << "\n";
return 0;
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:144:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for (int i = 0; i < ord.size(); i++)
| ~~^~~~~~~~~~~~
Village.cpp:146:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
146 | for (int i2 = 0; i2 < ord[i].size(); i2++)
| ~~~^~~~~~~~~~~~~~~
Village.cpp:149:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | if (p == ord[j].size()) {j = (j + 1) % ord.size(); p = 0;}
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2728 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2728 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2732 KB |
Output is correct |
13 |
Correct |
2 ms |
2724 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
3 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
3 ms |
2816 KB |
Output is correct |
6 |
Correct |
4 ms |
2816 KB |
Output is correct |
7 |
Correct |
3 ms |
2816 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
3 ms |
2816 KB |
Output is correct |
10 |
Correct |
3 ms |
2816 KB |
Output is correct |
11 |
Correct |
4 ms |
2836 KB |
Output is correct |
12 |
Correct |
4 ms |
2816 KB |
Output is correct |
13 |
Correct |
3 ms |
2816 KB |
Output is correct |
14 |
Correct |
3 ms |
2816 KB |
Output is correct |
15 |
Correct |
4 ms |
2816 KB |
Output is correct |
16 |
Correct |
4 ms |
2816 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
3 ms |
2816 KB |
Output is correct |
19 |
Correct |
3 ms |
2816 KB |
Output is correct |
20 |
Correct |
4 ms |
2816 KB |
Output is correct |
21 |
Correct |
4 ms |
2728 KB |
Output is correct |
22 |
Correct |
3 ms |
2816 KB |
Output is correct |
23 |
Correct |
3 ms |
2816 KB |
Output is correct |
24 |
Correct |
3 ms |
2816 KB |
Output is correct |
25 |
Correct |
3 ms |
2688 KB |
Output is correct |
26 |
Correct |
3 ms |
2816 KB |
Output is correct |
27 |
Correct |
3 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
4 ms |
2816 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2728 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2728 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2732 KB |
Output is correct |
13 |
Correct |
2 ms |
2724 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2708 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
3 ms |
2688 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2816 KB |
Output is correct |
22 |
Correct |
3 ms |
2816 KB |
Output is correct |
23 |
Correct |
4 ms |
2816 KB |
Output is correct |
24 |
Correct |
3 ms |
2816 KB |
Output is correct |
25 |
Correct |
3 ms |
2816 KB |
Output is correct |
26 |
Correct |
3 ms |
2816 KB |
Output is correct |
27 |
Correct |
3 ms |
2816 KB |
Output is correct |
28 |
Correct |
4 ms |
2836 KB |
Output is correct |
29 |
Correct |
4 ms |
2816 KB |
Output is correct |
30 |
Correct |
3 ms |
2816 KB |
Output is correct |
31 |
Correct |
3 ms |
2816 KB |
Output is correct |
32 |
Correct |
4 ms |
2816 KB |
Output is correct |
33 |
Correct |
4 ms |
2816 KB |
Output is correct |
34 |
Correct |
3 ms |
2816 KB |
Output is correct |
35 |
Correct |
3 ms |
2816 KB |
Output is correct |
36 |
Correct |
3 ms |
2816 KB |
Output is correct |
37 |
Correct |
4 ms |
2816 KB |
Output is correct |
38 |
Correct |
4 ms |
2728 KB |
Output is correct |
39 |
Correct |
3 ms |
2816 KB |
Output is correct |
40 |
Correct |
3 ms |
2816 KB |
Output is correct |
41 |
Correct |
3 ms |
2816 KB |
Output is correct |
42 |
Correct |
3 ms |
2688 KB |
Output is correct |
43 |
Correct |
3 ms |
2816 KB |
Output is correct |
44 |
Correct |
3 ms |
2688 KB |
Output is correct |
45 |
Correct |
3 ms |
2816 KB |
Output is correct |
46 |
Correct |
4 ms |
2816 KB |
Output is correct |
47 |
Correct |
3 ms |
2816 KB |
Output is correct |
48 |
Correct |
184 ms |
10076 KB |
Output is correct |
49 |
Correct |
217 ms |
10744 KB |
Output is correct |
50 |
Correct |
211 ms |
10828 KB |
Output is correct |
51 |
Correct |
157 ms |
8952 KB |
Output is correct |
52 |
Correct |
207 ms |
10696 KB |
Output is correct |
53 |
Correct |
179 ms |
9992 KB |
Output is correct |
54 |
Correct |
105 ms |
10852 KB |
Output is correct |
55 |
Correct |
258 ms |
19700 KB |
Output is correct |
56 |
Correct |
231 ms |
14836 KB |
Output is correct |
57 |
Correct |
227 ms |
13476 KB |
Output is correct |
58 |
Correct |
225 ms |
12148 KB |
Output is correct |
59 |
Correct |
203 ms |
10868 KB |
Output is correct |
60 |
Correct |
182 ms |
15836 KB |
Output is correct |
61 |
Correct |
185 ms |
11972 KB |
Output is correct |
62 |
Correct |
186 ms |
11148 KB |
Output is correct |
63 |
Correct |
171 ms |
10760 KB |
Output is correct |
64 |
Correct |
198 ms |
11112 KB |
Output is correct |
65 |
Correct |
190 ms |
11252 KB |
Output is correct |
66 |
Correct |
168 ms |
10768 KB |
Output is correct |
67 |
Correct |
131 ms |
9640 KB |
Output is correct |
68 |
Correct |
165 ms |
9856 KB |
Output is correct |
69 |
Correct |
190 ms |
11252 KB |
Output is correct |
70 |
Correct |
174 ms |
10732 KB |
Output is correct |
71 |
Correct |
119 ms |
8696 KB |
Output is correct |
72 |
Correct |
137 ms |
9464 KB |
Output is correct |
73 |
Correct |
189 ms |
11252 KB |
Output is correct |
74 |
Correct |
163 ms |
10532 KB |
Output is correct |
75 |
Correct |
211 ms |
10612 KB |
Output is correct |
76 |
Correct |
208 ms |
10616 KB |
Output is correct |
77 |
Correct |
190 ms |
10740 KB |
Output is correct |
78 |
Correct |
123 ms |
8188 KB |
Output is correct |
79 |
Correct |
148 ms |
9080 KB |
Output is correct |
80 |
Correct |
229 ms |
10616 KB |
Output is correct |
81 |
Correct |
206 ms |
10876 KB |
Output is correct |
82 |
Correct |
190 ms |
11004 KB |
Output is correct |