#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], Mx = 0, Mn = 0, n;
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 or ch[i] % 2 == 0)
continue;
Num2[i] = vec.back();
vec.push_back(i);
}
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=0;i<nei[rt].size();i++)
vec.push_back({-ch[nei[rt][i]], nei[rt][i]});
sort(begin(vec), end(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++];
for (int i=0;i<(-vec[0].first);i++)
Num[Vec[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<<"0 0\n1\n1\n";
return 0;
}
solve();
if (Num2[1] == 1)
swap(Num2[nei[1][0]], Num2[1]), Mn += 2;
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:56:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i=0;i<nei[rt].size();i++)
| ~^~~~~~~~~~~~~~~
Village.cpp:61: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]
61 | for (int i=1;i<vec.size();i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
2648 KB |
Output is correct |
5 |
Correct |
2 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 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Partially correct |
2 ms |
2656 KB |
Partially correct |
14 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
15 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
16 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
17 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
2 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
3 |
Partially correct |
1 ms |
2648 KB |
Partially correct |
4 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
5 |
Partially correct |
2 ms |
2800 KB |
Partially correct |
6 |
Partially correct |
2 ms |
2648 KB |
Partially correct |
7 |
Correct |
3 ms |
2908 KB |
Output is correct |
8 |
Partially correct |
2 ms |
2844 KB |
Partially correct |
9 |
Partially correct |
2 ms |
2908 KB |
Partially correct |
10 |
Partially correct |
2 ms |
2908 KB |
Partially correct |
11 |
Partially correct |
2 ms |
2648 KB |
Partially correct |
12 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
13 |
Correct |
2 ms |
2904 KB |
Output is correct |
14 |
Partially correct |
2 ms |
2836 KB |
Partially correct |
15 |
Partially correct |
2 ms |
2648 KB |
Partially correct |
16 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
17 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
18 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
19 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
20 |
Partially correct |
3 ms |
2652 KB |
Partially correct |
21 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
22 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
23 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
24 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
25 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
26 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
27 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
28 |
Partially correct |
2 ms |
2648 KB |
Partially correct |
29 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
30 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
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 |
2648 KB |
Output is correct |
5 |
Correct |
2 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 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Partially correct |
2 ms |
2656 KB |
Partially correct |
14 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
15 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
16 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
17 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
18 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
19 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
20 |
Partially correct |
1 ms |
2648 KB |
Partially correct |
21 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
22 |
Partially correct |
2 ms |
2800 KB |
Partially correct |
23 |
Partially correct |
2 ms |
2648 KB |
Partially correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Partially correct |
2 ms |
2844 KB |
Partially correct |
26 |
Partially correct |
2 ms |
2908 KB |
Partially correct |
27 |
Partially correct |
2 ms |
2908 KB |
Partially correct |
28 |
Partially correct |
2 ms |
2648 KB |
Partially correct |
29 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
30 |
Correct |
2 ms |
2904 KB |
Output is correct |
31 |
Partially correct |
2 ms |
2836 KB |
Partially correct |
32 |
Partially correct |
2 ms |
2648 KB |
Partially correct |
33 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
34 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
35 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
36 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
37 |
Partially correct |
3 ms |
2652 KB |
Partially correct |
38 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
39 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
40 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
41 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
42 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
43 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
44 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
45 |
Partially correct |
2 ms |
2648 KB |
Partially correct |
46 |
Partially correct |
2 ms |
2652 KB |
Partially correct |
47 |
Partially correct |
1 ms |
2652 KB |
Partially correct |
48 |
Partially correct |
68 ms |
9168 KB |
Partially correct |
49 |
Partially correct |
67 ms |
9840 KB |
Partially correct |
50 |
Partially correct |
85 ms |
9684 KB |
Partially correct |
51 |
Partially correct |
54 ms |
8232 KB |
Partially correct |
52 |
Partially correct |
71 ms |
9712 KB |
Partially correct |
53 |
Partially correct |
62 ms |
8872 KB |
Partially correct |
54 |
Correct |
73 ms |
12672 KB |
Output is correct |
55 |
Partially correct |
98 ms |
23236 KB |
Partially correct |
56 |
Incorrect |
98 ms |
16192 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |