Submission #575589

#TimeUsernameProblemLanguageResultExecution timeMemory
575589talant117408Village (BOI20_village)C++17
0 / 100
1 ms212 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; #define pb push_back #define mp make_pair #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() #define lb lower_bound #define ub upper_bound #define sz(v) int((v).size()) #define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define endl '\n' void solve() { int n; cin >> n; vector <int> graph[n]; for (int i = 0; i < n-1; i++) { int a, b; cin >> a >> b; a--; b--; graph[a].pb(b); graph[b].pb(a); } int cost[2]; cost[0] = cost[1] = 0; vector <vector <int>> rear(2, vector <int> (n, -1)); //~ int df = 0, vf = 0; //~ function <void(int, int, int)> find_furthest = [&](int v, int p, int dist) { //~ if (dist > df) { //~ df = dist; //~ vf = v; //~ } //~ for (auto to : graph[v]) { //~ if (to == p) continue; //~ find_furthest(to, v, dist+1); //~ } //~ }; //~ int d1, d2; //~ find_furthest(0, 0, 0); //~ d1 = vf; //~ df = vf = 0; //~ find_furthest(d1, d1, 0); //~ d2 = vf; //~ cost[1] = df; //~ rear[1][d1] = d2; //~ rear[1][d2] = d1; //~ vector <int> left; //~ for (int i = 0; i < n; i++) { //~ if (i == d1 || i == d2) { //~ continue; //~ } //~ left.pb(i); //~ } //~ for (int i = 0; i < n-2; i++) { //~ rear[0][left[i]] = left[(i+1)%(n-2)]; //~ } function <int(int, int)> find_smallest = [&](int v, int p) { vector <int> children; for (auto to : graph[v]) { if (to == p) continue; if (find_smallest(to, v)) { children.pb(to); } } if (sz(children)&1) { cost[0] += 2; rear[0][v] = children[0]; rear[0][children[0]] = v; for (int i = 1; i < sz(children); i += 2) { cost[0] += 4; rear[0][children[i]] = children[i+1]; rear[0][children[i+1]] = children[i]; } return 0; } else { if (sz(children)) { cost[0] += sz(children)*2; rear[0][v] = children[1]; rear[0][children[1]] = children[0]; rear[0][children[0]] = v; for (int i = 2; i < sz(children); i += 2) { cost[0] += 2; rear[0][children[i]] = children[i+1]; rear[0][children[i+1]] = children[i]; } return 0; } else if (v) return 1; if (sz(children)) { rear[0][v] = children[1]; rear[0][children[1]] = children[0]; rear[0][children[0]] = v; } else { cost[0] += 2; int a = graph[v][0], b = rear[0][rear[0][a]]; if (rear[0][b] == a) { rear[0][v] = a; rear[0][b] = v; } else { b = rear[0][a]; rear[0][v] = a; rear[0][b] = v; } } return 0; } }; find_smallest(0, 0); cout << cost[0] << ' ' << cost[1] << endl; for (int i = 0; i < 2; i++) { for (auto to : rear[i]) cout << to+1 << ' '; cout << endl; } } int main() { //~ do_not_disturb int t = 1; //~ cin >> t; while (t--) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...