Submission #1101984

#TimeUsernameProblemLanguageResultExecution timeMemory
1101984yoav_sVillage (BOI20_village)C++17
0 / 100
1004 ms848 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; 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); } vv distance(N, v(N)); for (ll i =0 ; i < N; i++) { vb visited(N, false); stack<p> dfs; dfs.emplace(i, 0); while (!dfs.empty()) { auto top = dfs.top(); dfs.pop(); if (visited[top.f]) continue; visited[top.f] = true; distance[i][top.f] = top.s; for (ll x : graph[top.f]) dfs.emplace(x, top.s + 1); } } v perm(N); for (ll i =0 ; i < N; i++) perm[i] = i; ll mini = INF, maxi = -INF; v miniPerm(N), maxiPerm(N); do { bool valid = true; for (ll i= 0 ; i < N; i++) if (perm[i] == i) valid = false; if (!valid) continue; ll ev = 0; for (ll i = 0; i < N; i++) ev += distance[i][perm[i]]; if (ev < mini) { mini = ev; copy(all(perm), miniPerm.begin()); } if (ev > maxi) { maxi = ev; copy(all(perm), maxiPerm.begin()); } } while (next_permutation(all(perm))); assert(mini <= N + 1); cout << mini << " " << maxi << "\n"; for (ll x : miniPerm) cout << x + 1 << " "; cout << "\n"; for (ll x : maxiPerm) cout << x + 1 << " "; cout << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...