# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1102045 |
2024-10-17T10:47:30 Z |
yoav_s |
Village (BOI20_village) |
C++17 |
|
1 ms |
336 KB |
#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 &dpIncludingRoot, v &dpExcludingRoot, vb &visited, v &chosenIncluding, v &chosenExcluding, vvp &sortedChildren, v &excludingWhat)
{
if (visited[i]) return;
visited[i] = true;
v children;
for (ll x : graph[i])
{
if (!visited[x])
{
children.pb(x);
solve(x, graph, dpIncludingRoot, dpExcludingRoot, visited, chosenIncluding, chosenExcluding, sortedChildren, excludingWhat);
}
}
ll res = 0;
for (ll x : children)
{
res += dpIncludingRoot[x];
}
dpExcludingRoot[i] = res;
chosenExcluding[i] = 0; excludingWhat[i] = i;
v diff;
for (ll x : children)
{
diff.pb(dpExcludingRoot[x] - dpIncludingRoot[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 < dpIncludingRoot[i])
{
dpIncludingRoot[i] = ev;
chosenIncluding[i] = amt;
}
if (amt >= 1)
{
ll ev = res + amt * 2;
if (ev < dpExcludingRoot[i])
{
dpExcludingRoot[i] = ev;
chosenExcluding[i] = amt;
excludingWhat[i] = (amt == 1)? excludingWhat[sortedChildren[i][0].s] : i;
}
}
amt++;
}
}
void recreatePerm(ll i, bool including, vv &graph, v &chosenIncluding, v &chosenExcluding, vvp &sortedChildren, v &res, v &excludingWhat)
{
ll amt = including? chosenIncluding[i] : chosenExcluding[i];
for (ll j = 0; j < amt; j++)
{
recreatePerm(sortedChildren[i][j].s, false, graph, chosenIncluding, chosenExcluding, sortedChildren, res, excludingWhat);
}
for (ll j = amt; j < sortedChildren[i].size(); j++)
{
recreatePerm(sortedChildren[i][j].s, true, graph, chosenIncluding, chosenExcluding, sortedChildren, res, excludingWhat);
}
v arr;
for (ll j = 0; j < amt; j++)
if (excludingWhat[sortedChildren[i][j].s] != excludingWhat[i] || including)
arr.pb(excludingWhat[sortedChildren[i][j].s]);
if (including || excludingWhat[i] != i)
{
arr.pb(i);
}
for (ll i = 0; i < arr.size(); i++) res[arr[i]] = arr[(i + 1) % arr.size()];
}
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 dpIncludingRoot(N, INF);
v dpExcludingRoot(N, INF);
v chosenIncluding(N, 0), chosenExcluding(N, 0);
vb visited(N, false);
vvp sortedChildren(N);
v excludingWhat(N);
solve(0, graph, dpIncludingRoot, dpExcludingRoot, visited, chosenIncluding, chosenExcluding, sortedChildren, excludingWhat);
v perm(N);
recreatePerm(0, true, graph, chosenIncluding, chosenExcluding, sortedChildren, perm, excludingWhat);
cout << dpIncludingRoot[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:101:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for (ll i = 0; i < arr.size(); i++) res[arr[i]] = arr[(i + 1) % arr.size()];
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
- |