#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
vector<int> g[100005], f[100005];
int t[100005], sz[100005], h[100005];
int nrf[100005];
int ans_mic, ans_mare, sol_mic[100005], sol_mare[100005];
void dfs(int nod)
{
sz[nod] = 1;
for (auto vecin : g[nod])
{
if (vecin != t[nod])
{
f[nod].push_back(vecin);
t[vecin] = nod;
h[vecin] = 1 + h[nod];
dfs(vecin);
sz[nod] += sz[vecin];
}
}
}
void solve_mic()
{
vector<bool> scos(n + 1);
for (int i = 1; i <= n; i++)
nrf[i] = f[i].size();
vector<int> nd;
for (int i = 1; i <= n; i++)
nd.push_back(i);
sort(nd.begin(), nd.end(), [](int A, int B)
{
return h[A] > h[B];
});
for (auto it : nd)
{
if (nrf[it] != 0)
{
vector<int> u;
for (auto itt : f[it])
{
if (!scos[itt])
{
scos[itt] = true;
u.push_back(itt);
}
}
scos[it] = true;
nrf[t[it]]--;
if (u.size() % 2 == 1)
{
u.push_back(it);
ans_mic -= 2;
for (int i = 0; i < u.size(); i += 2)
{
ans_mic += 4;
sol_mic[u[i]] = u[i + 1];
sol_mic[u[i + 1]] = u[i];
}
}
else
{
int x = it, y = u[0], z = u[1];
ans_mic += 4;
sol_mic[x] = y;
sol_mic[y] = z;
sol_mic[z] = x;
for (int i = 2; i < u.size(); i += 2)
{
ans_mic += 4;
sol_mic[u[i]] = u[i + 1];
sol_mic[u[i + 1]] = u[i];
}
}
}
else if (it == 1)
{
ans_mic += 2;
int x = f[1][0];
int y = sol_mic[x];
if (sol_mic[y] == x)
{
sol_mic[1] = x;
sol_mic[x] = y;
sol_mic[y] = 1;
}
else
{
int z = sol_mic[y];
sol_mic[1] = x;
sol_mic[x] = y;
sol_mic[y] = z;
sol_mic[z] = 1;
}
}
}
}
int find_centroid(int nod)
{
int ff = 0;
for (auto it : f[nod])
if (sz[it] > n / 2)
ff = it;
if (ff != 0)
return find_centroid(ff);
return nod;
}
vector<int> noduri;
void dfs2(int nod)
{
noduri.push_back(nod);
for (auto it : f[nod])
dfs2(it);
}
void solve_mare()
{
int c = find_centroid(1);
for (int i = 1; i <= n; i++)
{
f[i].clear();
t[i] = 0;
sz[i] = 0;
h[i] = 0;
}
dfs(c);
for (int i = 1; i <= n; i++)
ans_mare += 2 * h[i];
dfs2(c);
int ofs = (n + 1) / 2;
for (int i = 0; i < noduri.size(); i++)
{
int x = (i + ofs) % n;
sol_mare[noduri[i]] = noduri[x];
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
solve_mic();
solve_mare();
cout << ans_mic << ' ' << ans_mare << '\n';
for (int i = 1; i <= n; i++)
cout << sol_mic[i] << ' ';
cout << '\n';
for (int i = 1; i <= n; i++)
cout << sol_mare[i] << ' ';
return 0;
}
Compilation message
Village.cpp: In function 'void solve_mic()':
Village.cpp:61:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < u.size(); i += 2)
| ~~^~~~~~~~~~
Village.cpp:75:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 2; i < u.size(); i += 2)
| ~~^~~~~~~~~~
Village.cpp: In function 'void solve_mare()':
Village.cpp:141:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for (int i = 0; i < noduri.size(); i++)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5204 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5160 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
5092 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5160 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5308 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5212 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5464 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
5 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5204 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5160 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
2 ms |
5092 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5160 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5308 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5212 KB |
Output is correct |
41 |
Correct |
3 ms |
5212 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5464 KB |
Output is correct |
45 |
Correct |
3 ms |
5208 KB |
Output is correct |
46 |
Correct |
5 ms |
5212 KB |
Output is correct |
47 |
Correct |
2 ms |
5072 KB |
Output is correct |
48 |
Correct |
51 ms |
18176 KB |
Output is correct |
49 |
Correct |
58 ms |
19380 KB |
Output is correct |
50 |
Correct |
56 ms |
19388 KB |
Output is correct |
51 |
Correct |
49 ms |
16476 KB |
Output is correct |
52 |
Correct |
61 ms |
19404 KB |
Output is correct |
53 |
Correct |
48 ms |
17928 KB |
Output is correct |
54 |
Correct |
36 ms |
16192 KB |
Output is correct |
55 |
Correct |
75 ms |
27784 KB |
Output is correct |
56 |
Correct |
71 ms |
23212 KB |
Output is correct |
57 |
Correct |
74 ms |
21964 KB |
Output is correct |
58 |
Correct |
69 ms |
20916 KB |
Output is correct |
59 |
Correct |
59 ms |
19588 KB |
Output is correct |
60 |
Correct |
36 ms |
18484 KB |
Output is correct |
61 |
Correct |
38 ms |
18512 KB |
Output is correct |
62 |
Correct |
43 ms |
18840 KB |
Output is correct |
63 |
Correct |
39 ms |
18152 KB |
Output is correct |
64 |
Correct |
48 ms |
19384 KB |
Output is correct |
65 |
Correct |
41 ms |
19388 KB |
Output is correct |
66 |
Correct |
48 ms |
18148 KB |
Output is correct |
67 |
Correct |
30 ms |
15368 KB |
Output is correct |
68 |
Correct |
36 ms |
17156 KB |
Output is correct |
69 |
Correct |
40 ms |
19644 KB |
Output is correct |
70 |
Correct |
35 ms |
18404 KB |
Output is correct |
71 |
Correct |
28 ms |
15272 KB |
Output is correct |
72 |
Correct |
30 ms |
16476 KB |
Output is correct |
73 |
Correct |
47 ms |
19336 KB |
Output is correct |
74 |
Correct |
36 ms |
18172 KB |
Output is correct |
75 |
Correct |
58 ms |
19380 KB |
Output is correct |
76 |
Correct |
49 ms |
19128 KB |
Output is correct |
77 |
Correct |
51 ms |
19172 KB |
Output is correct |
78 |
Correct |
34 ms |
14812 KB |
Output is correct |
79 |
Correct |
39 ms |
15968 KB |
Output is correct |
80 |
Correct |
59 ms |
19380 KB |
Output is correct |
81 |
Correct |
52 ms |
19440 KB |
Output is correct |
82 |
Correct |
45 ms |
18644 KB |
Output is correct |