Submission #51850

#TimeUsernameProblemLanguageResultExecution timeMemory
51850mareksomNetwork (BOI15_net)C++17
100 / 100
1772 ms210956 KiB
#ifndef LOCAL #pragma GCC optimize ("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, int b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } constexpr int nax = 500 * 1000 + 105; int n; set<int> graf[nax]; vector<pair<int, int>> wynik; int Daj(int x, int oprocz) { assert(!graf[x].empty()); if (graf[x].find(oprocz) == graf[x].end()) { if ((int) graf[x].size() >= 2) return -1; return *graf[x].begin(); } if ((int) graf[x].size() >= 3) return -1; for (int y : graf[x]) { if (y != oprocz) { return y; } } assert(false); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; graf[a].insert(b); graf[b].insert(a); } set<int> liscie; for (int i = 1; i <= n; i++) { if ((int) graf[i].size() == 1) { liscie.insert(i); } } debug() << imie(liscie); while (true) { assert((int) liscie.size() >= 2); const int a = *liscie.begin(); liscie.erase(liscie.begin()); const int b = *liscie.begin(); liscie.erase(*liscie.begin()); if ((int) liscie.size() <= 1) { wynik.emplace_back(a, b); liscie.insert(b); if ((int) liscie.size() < 2) break; continue; } const int c = *liscie.begin(); liscie.erase(liscie.begin()); int s[3] = {a, b, c}; int w[3] = {a, b, c}; int p[3] = {-1, -1, -1}; while (true) { int daj[3]; for (int i = 0; i < 3; i++) { daj[i] = Daj(w[i], p[i]); } debug() << imie(range(s, s + 3)); debug() << imie(range(w, w + 3)); debug() << imie(range(p, p + 3)); debug() << imie(range(daj, daj + 3)); for (int i = 0; i < 3; i++) { for (int j = i + 1; j < 3; j++) { if (daj[i] == -1 and daj[j] == -1) { if (w[i] != w[j] or (int) graf[w[i]].size() >= 4) { debug() << imie("JEST"); wynik.emplace_back(s[i], s[j]); liscie.insert(s[3 ^ i ^ j]); graf[w[i]].erase(p[i]); graf[w[j]].erase(p[j]); goto out; } } } } for (int i = 0; i < 3; i++) { if (daj[i] != -1) { p[i] = w[i]; w[i] = daj[i]; } } } out:; } printf("%d\n", (int) wynik.size()); for (auto& it : wynik) { printf("%d %d\n", it.first, it.second); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...