Submission #922723

#TimeUsernameProblemLanguageResultExecution timeMemory
922723Alihan_8Network (BOI15_net)C++17
100 / 100
438 ms47056 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define ar array #define pb push_back #define ln '\n' #define int long long using i64 = long long; template <class F, class _S> bool chmin(F &u, const _S &v){ bool flag = false; if ( u > v ){ u = v; flag |= true; } return flag; } template <class F, class _S> bool chmax(F &u, const _S &v){ bool flag = false; if ( u < v ){ u = v; flag |= true; } return flag; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector <vector<int>> G(n); for ( int i = 1; i < n; i++ ){ int u, v; cin >> u >> v; --u, --v; G[u].pb(v), G[v].pb(u); } vector <int> lf; auto dfs = [&](auto dfs, int u, int p) -> void{ for ( auto &v: G[u] ){ if ( v != p ){ dfs(dfs, v, u); } } if ( G[u].size() == 1 ){ lf.pb(u); } }; dfs(dfs, 0, -1); if ( lf.size() & 1 ){ lf.pb(lf[0]); } int k = lf.size(); cout << k / 2 << ln; for ( int i = 0; i * 2 < k; i++ ){ cout << lf[i] + 1 << ' ' << lf[i + k / 2] + 1 << ln; } cout << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...