#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef pair<ll, ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
const ll INF = 1e12;
const ll mod = 1e9 + 7;
void solve(ll i, vv &graph, v &dpFullCycle, v &dpPath, vb &visited, v &chosenCycle, v &chosenPath, vvp &sortedChildren, v &pathEnd)
{
if (visited[i]) return;
visited[i] = true;
v children;
for (ll x : graph[i])
{
if (!visited[x])
{
children.pb(x);
solve(x, graph, dpFullCycle, dpPath, visited, chosenCycle, chosenPath, sortedChildren, pathEnd);
}
}
ll res = 0;
for (ll x : children)
{
res += dpFullCycle[x];
}
dpPath[i] = res;
chosenPath[i] = 0; pathEnd[i] = i;
v diff;
for (ll x : children)
{
diff.pb(dpPath[x] - dpFullCycle[x]);
sortedChildren[i].eb(diff.back(), x);
}
sort(all(diff));
sort(all(sortedChildren[i]));
ll amt = 1;
for (ll x : diff)
{
res += x;
ll ev = res + amt * 2;
if (ev < dpFullCycle[i])
{
dpFullCycle[i] = ev;
chosenCycle[i] = amt;
}
if (amt >= 1)
{
ll ev = res + amt * 2;
if (ev < dpPath[i])
{
dpPath[i] = ev;
chosenPath[i] = amt;
pathEnd[i] = pathEnd[sortedChildren[i][0].s];
}
}
amt++;
}
}
void recreatePerm(ll i, bool isCycle, vv &graph, v &chosenCycle, v &chosenPath, vvp &sortedChildren, v &res, v &pathEnd)
{
ll amt = isCycle? chosenCycle[i] : chosenPath[i];
for (ll j = 0; j < amt; j++)
{
recreatePerm(sortedChildren[i][j].s, false, graph, chosenCycle, chosenPath, sortedChildren, res, pathEnd);
}
for (ll j = amt; j < sortedChildren[i].size(); j++)
{
recreatePerm(sortedChildren[i][j].s, true, graph, chosenCycle, chosenPath, sortedChildren, res, pathEnd);
}
ll prev = i;
for (ll j = amt - 1; j >= 0; j--)
{
res[prev] = sortedChildren[i][j].s;
prev = pathEnd[sortedChildren[i][j].s];
}
if (isCycle) res[prev] = i;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll N;
cin >> N;
vv graph(N);
for (ll i = 0; i < N - 1; i++)
{
ll a, b;
cin >> a >> b; a--; b--;
graph[a].pb(b);
graph[b].pb(a);
}
v dpFullCycle(N, INF);
v dpPath(N, INF);
v chosenCycle(N, 0), chosenPath(N, 0);
vb visited(N, false);
vvp sortedChildren(N);
v pathEnd(N);
solve(0, graph, dpFullCycle, dpPath, visited, chosenCycle, chosenPath, sortedChildren, pathEnd);
v perm(N);
recreatePerm(0, true, graph, chosenCycle, chosenPath, sortedChildren, perm, pathEnd);
cout << dpFullCycle[0] << " " << 0 << "\n";
for (ll x : perm) cout << x + 1 << " ";
cout << "\n";
for (ll i = 0; i < N; i++) cout << "1 ";
cout << "\n";
return 0;
}
Compilation message
Village.cpp: In function 'void recreatePerm(ll, bool, vv&, v&, v&, vvp&, v&, v&)':
Village.cpp:89:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (ll j = amt; j < sortedChildren[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
336 KB |
Partially correct |
2 |
Partially correct |
0 ms |
336 KB |
Partially correct |
3 |
Partially correct |
1 ms |
336 KB |
Partially correct |
4 |
Partially correct |
1 ms |
336 KB |
Partially correct |
5 |
Partially correct |
1 ms |
504 KB |
Partially correct |
6 |
Partially correct |
1 ms |
336 KB |
Partially correct |
7 |
Partially correct |
1 ms |
336 KB |
Partially correct |
8 |
Partially correct |
1 ms |
336 KB |
Partially correct |
9 |
Partially correct |
1 ms |
336 KB |
Partially correct |
10 |
Partially correct |
1 ms |
336 KB |
Partially correct |
11 |
Partially correct |
1 ms |
336 KB |
Partially correct |
12 |
Partially correct |
1 ms |
336 KB |
Partially correct |
13 |
Partially correct |
1 ms |
336 KB |
Partially correct |
14 |
Partially correct |
1 ms |
336 KB |
Partially correct |
15 |
Partially correct |
1 ms |
336 KB |
Partially correct |
16 |
Partially correct |
1 ms |
336 KB |
Partially correct |
17 |
Partially correct |
1 ms |
504 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
504 KB |
Partially correct |
2 |
Partially correct |
1 ms |
336 KB |
Partially correct |
3 |
Partially correct |
1 ms |
336 KB |
Partially correct |
4 |
Partially correct |
1 ms |
592 KB |
Partially correct |
5 |
Partially correct |
1 ms |
592 KB |
Partially correct |
6 |
Partially correct |
1 ms |
592 KB |
Partially correct |
7 |
Partially correct |
1 ms |
648 KB |
Partially correct |
8 |
Partially correct |
1 ms |
592 KB |
Partially correct |
9 |
Partially correct |
1 ms |
592 KB |
Partially correct |
10 |
Partially correct |
1 ms |
592 KB |
Partially correct |
11 |
Partially correct |
1 ms |
592 KB |
Partially correct |
12 |
Partially correct |
1 ms |
760 KB |
Partially correct |
13 |
Partially correct |
1 ms |
592 KB |
Partially correct |
14 |
Partially correct |
1 ms |
768 KB |
Partially correct |
15 |
Partially correct |
1 ms |
760 KB |
Partially correct |
16 |
Partially correct |
1 ms |
592 KB |
Partially correct |
17 |
Partially correct |
1 ms |
592 KB |
Partially correct |
18 |
Partially correct |
1 ms |
592 KB |
Partially correct |
19 |
Partially correct |
1 ms |
592 KB |
Partially correct |
20 |
Partially correct |
1 ms |
756 KB |
Partially correct |
21 |
Partially correct |
1 ms |
592 KB |
Partially correct |
22 |
Partially correct |
1 ms |
592 KB |
Partially correct |
23 |
Partially correct |
1 ms |
592 KB |
Partially correct |
24 |
Partially correct |
1 ms |
592 KB |
Partially correct |
25 |
Partially correct |
1 ms |
336 KB |
Partially correct |
26 |
Partially correct |
1 ms |
592 KB |
Partially correct |
27 |
Partially correct |
1 ms |
336 KB |
Partially correct |
28 |
Partially correct |
1 ms |
592 KB |
Partially correct |
29 |
Partially correct |
1 ms |
592 KB |
Partially correct |
30 |
Partially correct |
1 ms |
592 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
336 KB |
Partially correct |
2 |
Partially correct |
0 ms |
336 KB |
Partially correct |
3 |
Partially correct |
1 ms |
336 KB |
Partially correct |
4 |
Partially correct |
1 ms |
336 KB |
Partially correct |
5 |
Partially correct |
1 ms |
504 KB |
Partially correct |
6 |
Partially correct |
1 ms |
336 KB |
Partially correct |
7 |
Partially correct |
1 ms |
336 KB |
Partially correct |
8 |
Partially correct |
1 ms |
336 KB |
Partially correct |
9 |
Partially correct |
1 ms |
336 KB |
Partially correct |
10 |
Partially correct |
1 ms |
336 KB |
Partially correct |
11 |
Partially correct |
1 ms |
336 KB |
Partially correct |
12 |
Partially correct |
1 ms |
336 KB |
Partially correct |
13 |
Partially correct |
1 ms |
336 KB |
Partially correct |
14 |
Partially correct |
1 ms |
336 KB |
Partially correct |
15 |
Partially correct |
1 ms |
336 KB |
Partially correct |
16 |
Partially correct |
1 ms |
336 KB |
Partially correct |
17 |
Partially correct |
1 ms |
504 KB |
Partially correct |
18 |
Partially correct |
1 ms |
504 KB |
Partially correct |
19 |
Partially correct |
1 ms |
336 KB |
Partially correct |
20 |
Partially correct |
1 ms |
336 KB |
Partially correct |
21 |
Partially correct |
1 ms |
592 KB |
Partially correct |
22 |
Partially correct |
1 ms |
592 KB |
Partially correct |
23 |
Partially correct |
1 ms |
592 KB |
Partially correct |
24 |
Partially correct |
1 ms |
648 KB |
Partially correct |
25 |
Partially correct |
1 ms |
592 KB |
Partially correct |
26 |
Partially correct |
1 ms |
592 KB |
Partially correct |
27 |
Partially correct |
1 ms |
592 KB |
Partially correct |
28 |
Partially correct |
1 ms |
592 KB |
Partially correct |
29 |
Partially correct |
1 ms |
760 KB |
Partially correct |
30 |
Partially correct |
1 ms |
592 KB |
Partially correct |
31 |
Partially correct |
1 ms |
768 KB |
Partially correct |
32 |
Partially correct |
1 ms |
760 KB |
Partially correct |
33 |
Partially correct |
1 ms |
592 KB |
Partially correct |
34 |
Partially correct |
1 ms |
592 KB |
Partially correct |
35 |
Partially correct |
1 ms |
592 KB |
Partially correct |
36 |
Partially correct |
1 ms |
592 KB |
Partially correct |
37 |
Partially correct |
1 ms |
756 KB |
Partially correct |
38 |
Partially correct |
1 ms |
592 KB |
Partially correct |
39 |
Partially correct |
1 ms |
592 KB |
Partially correct |
40 |
Partially correct |
1 ms |
592 KB |
Partially correct |
41 |
Partially correct |
1 ms |
592 KB |
Partially correct |
42 |
Partially correct |
1 ms |
336 KB |
Partially correct |
43 |
Partially correct |
1 ms |
592 KB |
Partially correct |
44 |
Partially correct |
1 ms |
336 KB |
Partially correct |
45 |
Partially correct |
1 ms |
592 KB |
Partially correct |
46 |
Partially correct |
1 ms |
592 KB |
Partially correct |
47 |
Partially correct |
1 ms |
592 KB |
Partially correct |
48 |
Partially correct |
50 ms |
15432 KB |
Partially correct |
49 |
Partially correct |
50 ms |
16968 KB |
Partially correct |
50 |
Partially correct |
55 ms |
16760 KB |
Partially correct |
51 |
Partially correct |
44 ms |
13076 KB |
Partially correct |
52 |
Partially correct |
52 ms |
16548 KB |
Partially correct |
53 |
Partially correct |
43 ms |
14920 KB |
Partially correct |
54 |
Partially correct |
40 ms |
22608 KB |
Partially correct |
55 |
Partially correct |
86 ms |
46672 KB |
Partially correct |
56 |
Partially correct |
75 ms |
31820 KB |
Partially correct |
57 |
Partially correct |
67 ms |
26956 KB |
Partially correct |
58 |
Partially correct |
64 ms |
22868 KB |
Partially correct |
59 |
Partially correct |
59 ms |
18324 KB |
Partially correct |
60 |
Partially correct |
44 ms |
18552 KB |
Partially correct |
61 |
Partially correct |
47 ms |
17732 KB |
Partially correct |
62 |
Partially correct |
44 ms |
17484 KB |
Partially correct |
63 |
Partially correct |
42 ms |
16460 KB |
Partially correct |
64 |
Partially correct |
53 ms |
18252 KB |
Partially correct |
65 |
Partially correct |
51 ms |
18380 KB |
Partially correct |
66 |
Partially correct |
44 ms |
16964 KB |
Partially correct |
67 |
Partially correct |
44 ms |
13360 KB |
Partially correct |
68 |
Partially correct |
44 ms |
15436 KB |
Partially correct |
69 |
Partially correct |
44 ms |
18252 KB |
Partially correct |
70 |
Partially correct |
41 ms |
17228 KB |
Partially correct |
71 |
Partially correct |
34 ms |
12636 KB |
Partially correct |
72 |
Partially correct |
38 ms |
14412 KB |
Partially correct |
73 |
Partially correct |
44 ms |
18344 KB |
Partially correct |
74 |
Partially correct |
40 ms |
16692 KB |
Partially correct |
75 |
Partially correct |
54 ms |
18216 KB |
Partially correct |
76 |
Partially correct |
60 ms |
17836 KB |
Partially correct |
77 |
Partially correct |
47 ms |
17996 KB |
Partially correct |
78 |
Partially correct |
33 ms |
12116 KB |
Partially correct |
79 |
Partially correct |
38 ms |
13652 KB |
Partially correct |
80 |
Partially correct |
53 ms |
18204 KB |
Partially correct |
81 |
Partially correct |
46 ms |
18252 KB |
Partially correct |
82 |
Partially correct |
46 ms |
17232 KB |
Partially correct |