#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 = 0; j < sortedChildren[i].size(); 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++)
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Village.cpp:94:22: 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]
94 | for (ll j = 0; j < sortedChildren[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
336 KB |
Partially correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
336 KB |
Partially correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |