Submission #1101973

#TimeUsernameProblemLanguageResultExecution timeMemory
1101973yoav_sVillage (BOI20_village)C++17
0 / 100
2 ms592 KiB
#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) { 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); } } ll res = 0; for (ll x : children) { res += dpIncludingRoot[x]; } dpExcludingRoot[i] = res; chosenExcluding[i] = 0; 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 >= 2) { ll ev = res + amt * 2; if (ev < dpExcludingRoot[i]) { dpExcludingRoot[i] = ev; chosenExcluding[i] = amt; } } amt++; } } void recreatePerm(ll i, bool including, vv &graph, v &chosenIncluding, v &chosenExcluding, vvp &sortedChildren, v &res) { 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); } for (ll j = amt; j < sortedChildren[i].size(); j++) { recreatePerm(sortedChildren[i][j].s, true, graph, chosenIncluding, chosenExcluding, sortedChildren, res); } v arr; for (ll j = 0; j < amt; j++) arr.pb(sortedChildren[i][j].s); if (including) { 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); solve(0, graph, dpIncludingRoot, dpExcludingRoot, visited, chosenIncluding, chosenExcluding, sortedChildren); v perm(N); recreatePerm(0, true, graph, chosenIncluding, chosenExcluding, sortedChildren, perm); 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 (stderr)

Village.cpp: In function 'void recreatePerm(ll, bool, vv&, v&, v&, vvp&, v&)':
Village.cpp:88: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]
   88 |     for (ll j = amt; j < sortedChildren[i].size(); j++)
      |                      ~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Village.cpp:98: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]
   98 |     for (ll i = 0; i < arr.size(); i++) res[arr[i]] = arr[(i + 1) % arr.size()];
      |                    ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...