#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define int long long
vector<vector<int>> graph;
vector<int> subsize;
vector<int> subtree;
int cent = -1;
int ds = 0;
int n;
vector<int> match;
vector<int> matched;
int dds = 0;
void dp(int i, int p)
{
vector<int> nodes;
for (int ele:graph[i])
{
if (ele == p) continue;
dp(ele, i);
if (matched[ele]==0) nodes.push_back(ele);
}
if (nodes.size()==0) return;
dds += nodes.size()*2;
match[i] = nodes[0];
for (int j = 0; j < nodes.size()-1; j++)
{
match[nodes[j]] = nodes[j+1];
}
match[nodes.back()] = i;
matched[i] = 1;
}
void dfs(int i, int p)
{
int sum = 0;
bool b = 1;
for (int ele:graph[i])
{
if (ele == p) continue;
dfs(ele, i);
if (subsize[ele]*2>n) b = 0;
subsize[i] += subsize[ele];
sum += subsize[ele];
}
sum = n-sum-1;
if (sum*2>n) b=0;
if (b) cent = i;
subsize[i]++;
}
void dist(int i, int p, int d)
{
for (int ele:graph[i])
{
if (ele == p) continue;
dist(ele, i, d+1);
}
ds += d;
}
void sub(int i, int p)
{
subtree.push_back(i);
for (int ele:graph[i])
{
if (ele == p) continue;
sub(ele, i);
}
}
bool comp(vector<int> &a, vector<int> &b)
{
if (a.size() > b.size()) return 1;
else return 0;
}
int32_t main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
graph.assign(n, vector<int>());
subsize.assign(n, 0);
match.assign(n, 0);
matched.assign(n, 0);
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(0, -1);
dp(cent, -1);
int mx = -1;
dist(cent, -1, 0);
mx = ds*2;
vector<vector<int>> subs;
for (int i = 0; i < graph[cent].size(); i++)
{
sub(graph[cent][i], cent);
subs.push_back(subtree);
subtree.clear();
}
subs.push_back(vector<int>(1, cent));
sort(subs.begin(), subs.end(), comp);
cout << dds << " " << mx << endl;
vector<int> left;
vector<int> ini;
vector<int> part(n, 0);
for (auto ele:subs)
{
for (int e:ele)
{
ini.push_back(e);
left.push_back(e);
}
}
for (int i = 0; i < subs[0].size(); i++)
{
part[ini[i]] = left.back();
left.pop_back();
}
int i = subs[0].size();
for (int ele:left)
{
part[ini[i]] = ele;
i++;
}
for (int i = 0; i <n; i++) cout << match[i]+1 << " ";
cout << endl;
for (int ele:part) cout << ele+1 << " ";
}
Compilation message
Village.cpp: In function 'void dp(long long int, long long int)':
Village.cpp:32: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]
32 | for (int j = 0; j < nodes.size()-1; j++)
| ~~^~~~~~~~~~~~~~~~
Village.cpp: In function 'int32_t main()':
Village.cpp:117: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]
117 | for (int i = 0; i < graph[cent].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~
Village.cpp:139: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]
139 | for (int i = 0; i < subs[0].size(); i++)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
452 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
1056 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
624 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Partially correct |
1 ms |
348 KB |
Partially correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Partially correct |
1 ms |
604 KB |
Partially correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
452 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
452 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
1056 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
624 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Partially correct |
1 ms |
348 KB |
Partially correct |
42 |
Correct |
1 ms |
600 KB |
Output is correct |
43 |
Partially correct |
1 ms |
604 KB |
Partially correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
600 KB |
Output is correct |
48 |
Correct |
60 ms |
14040 KB |
Output is correct |
49 |
Correct |
62 ms |
15512 KB |
Output is correct |
50 |
Correct |
56 ms |
15336 KB |
Output is correct |
51 |
Correct |
40 ms |
11780 KB |
Output is correct |
52 |
Correct |
54 ms |
14528 KB |
Output is correct |
53 |
Correct |
54 ms |
13976 KB |
Output is correct |
54 |
Correct |
29 ms |
10944 KB |
Output is correct |
55 |
Correct |
63 ms |
22412 KB |
Output is correct |
56 |
Correct |
61 ms |
19168 KB |
Output is correct |
57 |
Correct |
62 ms |
17088 KB |
Output is correct |
58 |
Correct |
61 ms |
16652 KB |
Output is correct |
59 |
Correct |
63 ms |
15128 KB |
Output is correct |
60 |
Correct |
59 ms |
20532 KB |
Output is correct |
61 |
Correct |
48 ms |
16480 KB |
Output is correct |
62 |
Correct |
41 ms |
15408 KB |
Output is correct |
63 |
Correct |
42 ms |
14524 KB |
Output is correct |
64 |
Correct |
47 ms |
14968 KB |
Output is correct |
65 |
Correct |
50 ms |
15660 KB |
Output is correct |
66 |
Correct |
41 ms |
14420 KB |
Output is correct |
67 |
Correct |
42 ms |
12680 KB |
Output is correct |
68 |
Correct |
38 ms |
12900 KB |
Output is correct |
69 |
Correct |
55 ms |
15440 KB |
Output is correct |
70 |
Correct |
51 ms |
14272 KB |
Output is correct |
71 |
Correct |
36 ms |
10920 KB |
Output is correct |
72 |
Correct |
31 ms |
12072 KB |
Output is correct |
73 |
Correct |
45 ms |
16016 KB |
Output is correct |
74 |
Correct |
49 ms |
13976 KB |
Output is correct |
75 |
Correct |
58 ms |
15788 KB |
Output is correct |
76 |
Partially correct |
63 ms |
15344 KB |
Partially correct |
77 |
Correct |
62 ms |
14776 KB |
Output is correct |
78 |
Partially correct |
34 ms |
10168 KB |
Partially correct |
79 |
Correct |
36 ms |
11684 KB |
Output is correct |
80 |
Correct |
59 ms |
15928 KB |
Output is correct |
81 |
Partially correct |
45 ms |
14656 KB |
Partially correct |
82 |
Correct |
40 ms |
14832 KB |
Output is correct |