#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){
ch[u] = 1;
for (int i : nei[u]){
if (i == p)
continue;
dfs(i, u);
ch[u] += ch[i];
Mx += min(n - ch[i], ch[i]);
}
}
void dfs2(int u, int p){
vector<int> vec = {u};
for (int i : nei[u]){
if (i == p)
continue;
dfs2(i, u);
if (Num2[i] == i){
int k = Num2[i];
Num2[i] = vec.back();
vec.push_back(k);
Mn += 2;
}
}
Num2[u] = vec.back();
if (u == 1 and Num2[u] == 1)
swap(Num2[u], Num2[nei[u][0]]), Mn += 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);
dfs2(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:70: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]
70 | for (int i=1;i<vec.size();i++){
| ~^~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:92:11: warning: division by zero [-Wdiv-by-zero]
92 | cout<<1 / 0;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 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 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 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 |
2 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2788 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2832 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2840 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2880 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 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 |
3 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 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 |
2648 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2648 KB |
Output is correct |
29 |
Correct |
2 ms |
2656 KB |
Output is correct |
30 |
Correct |
2 ms |
2656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 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 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 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 |
2 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2788 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2832 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2840 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2648 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 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 |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2880 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 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 |
3 ms |
2652 KB |
Output is correct |
39 |
Correct |
2 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 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 |
2648 KB |
Output is correct |
44 |
Correct |
2 ms |
2652 KB |
Output is correct |
45 |
Correct |
2 ms |
2648 KB |
Output is correct |
46 |
Correct |
2 ms |
2656 KB |
Output is correct |
47 |
Correct |
2 ms |
2656 KB |
Output is correct |
48 |
Correct |
65 ms |
8712 KB |
Output is correct |
49 |
Correct |
70 ms |
9328 KB |
Output is correct |
50 |
Correct |
72 ms |
9452 KB |
Output is correct |
51 |
Correct |
66 ms |
8064 KB |
Output is correct |
52 |
Correct |
69 ms |
9600 KB |
Output is correct |
53 |
Correct |
65 ms |
8932 KB |
Output is correct |
54 |
Correct |
50 ms |
12884 KB |
Output is correct |
55 |
Correct |
93 ms |
24404 KB |
Output is correct |
56 |
Correct |
89 ms |
16588 KB |
Output is correct |
57 |
Correct |
81 ms |
14152 KB |
Output is correct |
58 |
Correct |
84 ms |
12160 KB |
Output is correct |
59 |
Correct |
71 ms |
10068 KB |
Output is correct |
60 |
Correct |
69 ms |
10036 KB |
Output is correct |
61 |
Correct |
58 ms |
10012 KB |
Output is correct |
62 |
Correct |
65 ms |
10092 KB |
Output is correct |
63 |
Correct |
58 ms |
9616 KB |
Output is correct |
64 |
Correct |
72 ms |
9968 KB |
Output is correct |
65 |
Correct |
56 ms |
10272 KB |
Output is correct |
66 |
Correct |
52 ms |
9884 KB |
Output is correct |
67 |
Correct |
44 ms |
8068 KB |
Output is correct |
68 |
Correct |
49 ms |
8908 KB |
Output is correct |
69 |
Correct |
55 ms |
10100 KB |
Output is correct |
70 |
Correct |
57 ms |
9724 KB |
Output is correct |
71 |
Correct |
39 ms |
8052 KB |
Output is correct |
72 |
Correct |
50 ms |
8512 KB |
Output is correct |
73 |
Correct |
60 ms |
10148 KB |
Output is correct |
74 |
Correct |
50 ms |
9580 KB |
Output is correct |
75 |
Correct |
72 ms |
9648 KB |
Output is correct |
76 |
Correct |
67 ms |
9584 KB |
Output is correct |
77 |
Correct |
62 ms |
9824 KB |
Output is correct |
78 |
Correct |
44 ms |
7336 KB |
Output is correct |
79 |
Correct |
53 ms |
8320 KB |
Output is correct |
80 |
Correct |
70 ms |
9652 KB |
Output is correct |
81 |
Correct |
64 ms |
9920 KB |
Output is correct |
82 |
Correct |
69 ms |
9952 KB |
Output is correct |