#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
vector<int> nei[N], lst;
int Num[N], Num2[N], ch[N], n;
long long Mx = 0, Mn = 0;
void dfs(int u, int p, int c = 0){
ch[u] = 1;
for (int i : nei[u]){
if (i == p)
continue;
dfs(i, u, c);
ch[u] += ch[i];
Mx += min(n - ch[i], ch[i]);
}
if (c == 0)
return;
vector<int> vec = {u};
for (int i : nei[u]){
if (i == p)
continue;
if (Num2[i] == i or (u == 1 and vec.size() == 1)){
int k = Num2[i];
Num2[i] = vec.back();
vec.push_back(k);
}
}
Num2[u] = vec.back();
Mn += (vec.size() - 1) * 2;
}
int find_cent(int u, int p){
for (int i : nei[u])
if (i != p and ch[i] * 2 > ch[1])
return find_cent(i, u);
return u;
}
void get(int u, int p){
for (int i : nei[u])
if (i != p)
get(i, u);
lst.push_back(u);
}
vector<int> get(int a, int b, int c){
lst.clear();
get(a, b);
return lst;
}
void solve(){
dfs(1, 1, 1);
int rt = find_cent(1, 1), ind = 0;
dfs(rt, rt);
vector<pair<int,int>> vec;
for (int i : nei[rt])
vec.push_back({ch[i], i});
sort(rbegin(vec), rend(vec));
vector<int> Vec =get(vec[0].second, rt, 1);
for (int i=1;i<vec.size();i++){
vector<int> v = get(vec[i].second, rt, 1);
for (int j : v)
Num[j] = Vec[ind++], Vec.push_back(j);
}
Vec.push_back(rt);
Num[rt] = Vec[ind++];
get(vec[0].second, rt, 1);
for (int i : lst)
Num[i] = Vec[ind++];
}
int main(){
cin>>n;
for (int i=1, a, b;i<n;i++){
cin>>a>>b;
nei[a].push_back(b);
nei[b].push_back(a);
}
if (n == 1){
cout<<1 / 0;
return 0;
}
solve();
cout<<Mn<<" "<<Mx<<'\n';
for (int i=1;i<=n;i++)
cout<<Num2[i]<<' ';
cout<<'\n';
for (int i=1;i<=n;i++)
cout<<Num[i]<<' ';
cout<<'\n';
}
Compilation message
Village.cpp: In function 'void solve()':
Village.cpp:66:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int i=1;i<vec.size();i++){
| ~^~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:88:11: warning: division by zero [-Wdiv-by-zero]
88 | cout<<1 / 0;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
16 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
7 |
Partially correct |
2 ms |
2908 KB |
Partially correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2804 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2744 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
16 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
24 |
Partially correct |
2 ms |
2908 KB |
Partially correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2804 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2648 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
2 ms |
2652 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
2 ms |
2652 KB |
Output is correct |
42 |
Correct |
2 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2652 KB |
Output is correct |
44 |
Correct |
2 ms |
2744 KB |
Output is correct |
45 |
Correct |
2 ms |
2652 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
48 |
Partially correct |
62 ms |
8148 KB |
Partially correct |
49 |
Correct |
82 ms |
8564 KB |
Output is correct |
50 |
Correct |
63 ms |
8472 KB |
Output is correct |
51 |
Correct |
50 ms |
7148 KB |
Output is correct |
52 |
Partially correct |
62 ms |
8528 KB |
Partially correct |
53 |
Correct |
57 ms |
8000 KB |
Output is correct |
54 |
Partially correct |
39 ms |
11440 KB |
Partially correct |
55 |
Correct |
81 ms |
20676 KB |
Output is correct |
56 |
Correct |
74 ms |
14400 KB |
Output is correct |
57 |
Correct |
74 ms |
12348 KB |
Output is correct |
58 |
Correct |
70 ms |
10672 KB |
Output is correct |
59 |
Correct |
65 ms |
8872 KB |
Output is correct |
60 |
Correct |
76 ms |
9036 KB |
Output is correct |
61 |
Correct |
58 ms |
8760 KB |
Output is correct |
62 |
Correct |
66 ms |
9068 KB |
Output is correct |
63 |
Correct |
55 ms |
8588 KB |
Output is correct |
64 |
Correct |
71 ms |
8784 KB |
Output is correct |
65 |
Correct |
61 ms |
8992 KB |
Output is correct |
66 |
Correct |
53 ms |
8856 KB |
Output is correct |
67 |
Correct |
48 ms |
7504 KB |
Output is correct |
68 |
Correct |
51 ms |
8140 KB |
Output is correct |
69 |
Correct |
59 ms |
9160 KB |
Output is correct |
70 |
Correct |
57 ms |
8728 KB |
Output is correct |
71 |
Correct |
47 ms |
7216 KB |
Output is correct |
72 |
Correct |
51 ms |
7668 KB |
Output is correct |
73 |
Correct |
58 ms |
9128 KB |
Output is correct |
74 |
Correct |
62 ms |
8692 KB |
Output is correct |
75 |
Correct |
66 ms |
8376 KB |
Output is correct |
76 |
Correct |
66 ms |
8560 KB |
Output is correct |
77 |
Correct |
58 ms |
8640 KB |
Output is correct |
78 |
Correct |
42 ms |
6568 KB |
Output is correct |
79 |
Correct |
50 ms |
7304 KB |
Output is correct |
80 |
Correct |
69 ms |
8372 KB |
Output is correct |
81 |
Correct |
59 ms |
8700 KB |
Output is correct |
82 |
Correct |
58 ms |
8728 KB |
Output is correct |