#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define ll long long
#define int ll
#define eps 1e-7
#define all(x) ((x).begin()),((x).end())
#define usecppio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
using pii = pair<int, int>;
int n;
vector<vector<int>> G;
vector<pii> edges;
int minval, maxval;
int minarr[101010], maxarr[101010];
vector <int> tp;
int par[101010];
int sub[101010];
int level[101010];
void aux(int rt, int p)
{
level[rt] = level[p]+1;
sub[rt]++;
par[rt] = p;
tp.push_back(rt);
for (int nxt : G[rt])
{
if (nxt != p)
{
aux(nxt, rt);
sub[rt] += sub[nxt];
}
}
}
void solve_min()
{
int cnt = 0;
aux(1, 0);
par[1] = G[1].front();
while(!tp.empty())
{
int cur = tp.back();
if (minarr[cur] == cur)
{
swap(minarr[cur], minarr[par[cur]]);
cnt += 2;
}
tp.pop_back();
}
minval = cnt;
par[1] = 0;
return;
}
pair<bool, int> is_center(int rt, int p)
{
int max_sub = 0, sum_sub = 0, index = rt;
for (int nxt : G[rt])
{
if (nxt != p)
{
if (max_sub <= sub[nxt])
{
max_sub = sub[nxt];
index = nxt;
}
sum_sub += sub[nxt];
}
}
if (n-1-sum_sub >= max_sub)
{
max_sub = n-1-sum_sub;
index = par[rt];
}
return {max_sub <= n/2, index};
}
int center(int rt, int p)
{
auto u = is_center(rt, p);
if (u.first) return rt;
else
{
return center(u.second, rt);
}
}
vector <deque<int>> D;
deque <int> seats;
void alloc_seat(int rt, int p)
{
seats.push_back(rt);
for (int nxt : G[rt])
if (nxt != p)
alloc_seat(nxt, rt);
}
void solve_max()
{
int cent = center(1, 0);
for (pii e : edges)
{
int a = e.first, b = e.second;
if (level[a] > level[b]) swap(a, b);
int passes = 2*min(sub[b], n-sub[b]);
maxval += passes;
}
memset(sub, 0, sizeof(sub));
memset(par, 0, sizeof(par));
memset(level, 0, sizeof(level));
aux(cent, 0);
sort(all(G[cent]), [](int a, int b)->bool{return sub[a] > sub[b];});
for (int head : G[cent])
{
alloc_seat(head, cent);
D.push_back(seats);
seats.clear();
}
if (n%2 == 0)
D.push_back({cent});
/*for (int i = 0; i<D.size(); i++)
{
printf("D[%lld] : ",i);
for (auto it:D[i])
printf("%lld ",it);
printf("\n");
}*/
for (int i = 0; i<D.size(); i++)
{
if (D[i].empty()) continue;
int cur = i+1;
while (!D[i].empty())
{
while ((D[cur].size() > 0) && (D[i].size() > 0))
{
if (D[cur].empty())
break;
swap(maxarr[D[cur].front()], maxarr[D[i].front()]);
D[cur].pop_front();
D[i].pop_front();
}
cur++;
}
}
if (n%2 != 0)
swap(maxarr[cent], maxarr[G[cent].front()]);
}
int32_t main()
{
usecppio
cin >> n;
G.resize(n+5);
for (int i = 1; i<n; i++)
{
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
edges.push_back({u, v});
}
for (int i = 1; i<=n; i++)
minarr[i] = maxarr[i] = i;
solve_min();
//solve_max();
cout << minval << ' ' << maxval << '\n';
for (int i = 1; i<=n; i++)
cout << minarr[i] << ' ';
cout << '\n';
for (int i = 1; i<=n; i++)
cout << maxarr[i] << ' ';
cout << '\n';
}
Compilation message
Village.cpp: In function 'void solve_max()':
Village.cpp:123:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::deque<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (int i = 0; i<D.size(); i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
384 KB |
Partially correct |
2 |
Partially correct |
1 ms |
384 KB |
Partially correct |
3 |
Partially correct |
0 ms |
384 KB |
Partially correct |
4 |
Partially correct |
0 ms |
384 KB |
Partially correct |
5 |
Partially correct |
0 ms |
384 KB |
Partially correct |
6 |
Partially correct |
1 ms |
384 KB |
Partially correct |
7 |
Partially correct |
0 ms |
384 KB |
Partially correct |
8 |
Partially correct |
0 ms |
384 KB |
Partially correct |
9 |
Partially correct |
0 ms |
384 KB |
Partially correct |
10 |
Partially correct |
0 ms |
384 KB |
Partially correct |
11 |
Partially correct |
0 ms |
384 KB |
Partially correct |
12 |
Partially correct |
0 ms |
384 KB |
Partially correct |
13 |
Partially correct |
0 ms |
384 KB |
Partially correct |
14 |
Partially correct |
0 ms |
384 KB |
Partially correct |
15 |
Partially correct |
0 ms |
384 KB |
Partially correct |
16 |
Partially correct |
0 ms |
384 KB |
Partially correct |
17 |
Partially correct |
0 ms |
384 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
384 KB |
Partially correct |
2 |
Partially correct |
1 ms |
512 KB |
Partially correct |
3 |
Partially correct |
1 ms |
384 KB |
Partially correct |
4 |
Partially correct |
1 ms |
512 KB |
Partially correct |
5 |
Partially correct |
1 ms |
512 KB |
Partially correct |
6 |
Partially correct |
1 ms |
512 KB |
Partially correct |
7 |
Partially correct |
1 ms |
512 KB |
Partially correct |
8 |
Partially correct |
1 ms |
512 KB |
Partially correct |
9 |
Partially correct |
1 ms |
512 KB |
Partially correct |
10 |
Partially correct |
1 ms |
512 KB |
Partially correct |
11 |
Partially correct |
1 ms |
512 KB |
Partially correct |
12 |
Partially correct |
1 ms |
640 KB |
Partially correct |
13 |
Partially correct |
1 ms |
512 KB |
Partially correct |
14 |
Partially correct |
1 ms |
512 KB |
Partially correct |
15 |
Partially correct |
1 ms |
512 KB |
Partially correct |
16 |
Partially correct |
1 ms |
640 KB |
Partially correct |
17 |
Partially correct |
1 ms |
512 KB |
Partially correct |
18 |
Partially correct |
1 ms |
512 KB |
Partially correct |
19 |
Partially correct |
1 ms |
512 KB |
Partially correct |
20 |
Partially correct |
1 ms |
512 KB |
Partially correct |
21 |
Partially correct |
1 ms |
512 KB |
Partially correct |
22 |
Partially correct |
2 ms |
512 KB |
Partially correct |
23 |
Partially correct |
1 ms |
512 KB |
Partially correct |
24 |
Partially correct |
1 ms |
512 KB |
Partially correct |
25 |
Partially correct |
1 ms |
384 KB |
Partially correct |
26 |
Partially correct |
1 ms |
512 KB |
Partially correct |
27 |
Partially correct |
1 ms |
384 KB |
Partially correct |
28 |
Partially correct |
1 ms |
512 KB |
Partially correct |
29 |
Partially correct |
1 ms |
512 KB |
Partially correct |
30 |
Partially correct |
1 ms |
512 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
384 KB |
Partially correct |
2 |
Partially correct |
1 ms |
384 KB |
Partially correct |
3 |
Partially correct |
0 ms |
384 KB |
Partially correct |
4 |
Partially correct |
0 ms |
384 KB |
Partially correct |
5 |
Partially correct |
0 ms |
384 KB |
Partially correct |
6 |
Partially correct |
1 ms |
384 KB |
Partially correct |
7 |
Partially correct |
0 ms |
384 KB |
Partially correct |
8 |
Partially correct |
0 ms |
384 KB |
Partially correct |
9 |
Partially correct |
0 ms |
384 KB |
Partially correct |
10 |
Partially correct |
0 ms |
384 KB |
Partially correct |
11 |
Partially correct |
0 ms |
384 KB |
Partially correct |
12 |
Partially correct |
0 ms |
384 KB |
Partially correct |
13 |
Partially correct |
0 ms |
384 KB |
Partially correct |
14 |
Partially correct |
0 ms |
384 KB |
Partially correct |
15 |
Partially correct |
0 ms |
384 KB |
Partially correct |
16 |
Partially correct |
0 ms |
384 KB |
Partially correct |
17 |
Partially correct |
0 ms |
384 KB |
Partially correct |
18 |
Partially correct |
1 ms |
384 KB |
Partially correct |
19 |
Partially correct |
1 ms |
512 KB |
Partially correct |
20 |
Partially correct |
1 ms |
384 KB |
Partially correct |
21 |
Partially correct |
1 ms |
512 KB |
Partially correct |
22 |
Partially correct |
1 ms |
512 KB |
Partially correct |
23 |
Partially correct |
1 ms |
512 KB |
Partially correct |
24 |
Partially correct |
1 ms |
512 KB |
Partially correct |
25 |
Partially correct |
1 ms |
512 KB |
Partially correct |
26 |
Partially correct |
1 ms |
512 KB |
Partially correct |
27 |
Partially correct |
1 ms |
512 KB |
Partially correct |
28 |
Partially correct |
1 ms |
512 KB |
Partially correct |
29 |
Partially correct |
1 ms |
640 KB |
Partially correct |
30 |
Partially correct |
1 ms |
512 KB |
Partially correct |
31 |
Partially correct |
1 ms |
512 KB |
Partially correct |
32 |
Partially correct |
1 ms |
512 KB |
Partially correct |
33 |
Partially correct |
1 ms |
640 KB |
Partially correct |
34 |
Partially correct |
1 ms |
512 KB |
Partially correct |
35 |
Partially correct |
1 ms |
512 KB |
Partially correct |
36 |
Partially correct |
1 ms |
512 KB |
Partially correct |
37 |
Partially correct |
1 ms |
512 KB |
Partially correct |
38 |
Partially correct |
1 ms |
512 KB |
Partially correct |
39 |
Partially correct |
2 ms |
512 KB |
Partially correct |
40 |
Partially correct |
1 ms |
512 KB |
Partially correct |
41 |
Partially correct |
1 ms |
512 KB |
Partially correct |
42 |
Partially correct |
1 ms |
384 KB |
Partially correct |
43 |
Partially correct |
1 ms |
512 KB |
Partially correct |
44 |
Partially correct |
1 ms |
384 KB |
Partially correct |
45 |
Partially correct |
1 ms |
512 KB |
Partially correct |
46 |
Partially correct |
1 ms |
512 KB |
Partially correct |
47 |
Partially correct |
1 ms |
512 KB |
Partially correct |
48 |
Partially correct |
77 ms |
14824 KB |
Partially correct |
49 |
Partially correct |
89 ms |
16104 KB |
Partially correct |
50 |
Partially correct |
85 ms |
16104 KB |
Partially correct |
51 |
Partially correct |
64 ms |
12904 KB |
Partially correct |
52 |
Partially correct |
85 ms |
15976 KB |
Partially correct |
53 |
Partially correct |
77 ms |
14440 KB |
Partially correct |
54 |
Partially correct |
42 ms |
10732 KB |
Partially correct |
55 |
Partially correct |
101 ms |
21480 KB |
Partially correct |
56 |
Partially correct |
102 ms |
18872 KB |
Partially correct |
57 |
Partially correct |
99 ms |
17768 KB |
Partially correct |
58 |
Partially correct |
98 ms |
17212 KB |
Partially correct |
59 |
Partially correct |
93 ms |
16232 KB |
Partially correct |
60 |
Partially correct |
64 ms |
16224 KB |
Partially correct |
61 |
Partially correct |
66 ms |
16364 KB |
Partially correct |
62 |
Partially correct |
71 ms |
16488 KB |
Partially correct |
63 |
Partially correct |
67 ms |
15556 KB |
Partially correct |
64 |
Partially correct |
78 ms |
16616 KB |
Partially correct |
65 |
Partially correct |
75 ms |
16616 KB |
Partially correct |
66 |
Partially correct |
71 ms |
15464 KB |
Partially correct |
67 |
Partially correct |
52 ms |
12392 KB |
Partially correct |
68 |
Partially correct |
63 ms |
14312 KB |
Partially correct |
69 |
Partially correct |
75 ms |
16744 KB |
Partially correct |
70 |
Partially correct |
67 ms |
15720 KB |
Partially correct |
71 |
Partially correct |
52 ms |
12008 KB |
Partially correct |
72 |
Partially correct |
56 ms |
13556 KB |
Partially correct |
73 |
Partially correct |
76 ms |
16752 KB |
Partially correct |
74 |
Partially correct |
64 ms |
15336 KB |
Partially correct |
75 |
Partially correct |
87 ms |
16616 KB |
Partially correct |
76 |
Partially correct |
77 ms |
15976 KB |
Partially correct |
77 |
Partially correct |
85 ms |
16112 KB |
Partially correct |
78 |
Partially correct |
52 ms |
11368 KB |
Partially correct |
79 |
Partially correct |
57 ms |
12904 KB |
Partially correct |
80 |
Partially correct |
87 ms |
16616 KB |
Partially correct |
81 |
Partially correct |
77 ms |
16104 KB |
Partially correct |
82 |
Partially correct |
73 ms |
16360 KB |
Partially correct |