Submission #583425

#TimeUsernameProblemLanguageResultExecution timeMemory
583425TheLimePixelNetwork (BOI15_net)C++17
100 / 100
542 ms47140 KiB
#include "bits/stdc++.h" using namespace std; vector<vector<int>> graph; vector<int> leaves_from; // Brojot na listovi vo sekoe poddrvo vector<int> leaves; // indeksite na listovite int leaf_count, res_count; // Brojot na listovi i brojt na potrebni vrski // Funkcija koja rekurentno gi broi listovite vo sekoe poddrvo void count_leaves(int node, int parent) { if (graph[node].size() == 1) leaves_from[node] = 1; for (auto adj : graph[node]) { if (adj == parent) continue; count_leaves(adj, node); leaves_from[node] += leaves_from[adj]; } } // Funkcija za baranje na srednoto teme int find_mid(int node) { int parent = 0; for (auto adj : graph[node]) if (adj != parent && leaves_from[adj] > leaf_count / 2) { parent = node; node = adj; } return node; } // Funkcija koja rekurentno gi dodava listovite void add_leaves(int node, int parent) { if (graph[node].size() == 1) leaves.push_back(node); for (auto adj : graph[node]) if (adj != parent) add_leaves(adj, node); } // Funkcija koja go deli drvoto na poddrva i gi dodava nivnite listovi na nizata int split(int n) { count_leaves(1, 0); int mid = find_mid(1); for (auto adj : graph[mid]) add_leaves(adj, mid); return mid; } // Glavnata funkcija void run() { int n; cin >> n; // Se spravuvame so posebni sluchai if (n == 1) { cout << 0; return; } if (n == 2) { cout << "1\n2"; return; } graph = vector<vector<int>>(n + 1); leaves_from = vector<int>(n + 1); // Go inicijalizirame drvoto for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } // Gi broime listovite, a so toa i brojot na potrebni vrski for (int i = 1; i <= n; i++) if (graph[i].size() == 1) leaf_count++; res_count = (leaf_count + 1) / 2; cout << res_count; leaves.reserve(2 * res_count); // Go delime drvoto na poddrva i gi dodavame nivnite listovi split(n); if (leaf_count & 1) leaves.push_back(leaves.front()); // Gi pechatime soodvetnite parovi na listovi for (int i = 0; i < res_count; i++) cout << '\n' << leaves[i] << ' ' << leaves[res_count + i]; } // Main funkcija auto main() -> int { ios_base::sync_with_stdio(false); cin.tie(NULL); run(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...