Submission #394654

#TimeUsernameProblemLanguageResultExecution timeMemory
394654saniyar_krmiNetwork (BOI15_net)C++14
0 / 100
14 ms23756 KiB
// Possible Overdose for this coder #include <bits/stdc++.h> #define put(x) cerr << #x << ": " << x << '\n' #define line() cerr << "**************\n" //#define F first //#define S second //#define mul(x, y) (((x) * (y)) %mod) //#define bit(i, j) (((i)>>(j)) &1) //#define left(id) ((id<<1) + 1) //#define right(id) ((id<<1) + 2) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; const int maxn = 5e5 + 10; int n; vector <int> G[maxn]; bool mark[maxn]; int leaves[maxn], num; void dfs(int v){ mark[v] = 1; if(G[v].size() == 1) leaves[v] = 1; for(int u: G[v]){ if(mark[u]) continue; dfs(u); leaves[v] += leaves[u]; } } vector <int> s[maxn]; void make(int v, int x){ mark[v] = 1; if(G[v].size() == 1) s[x].push_back(v); for(int u: G[v]){ if(mark[u]) continue; make(u, x); } } int find(int v){ mark[v] = 1; for(int u: G[v]){ if(mark[u]) continue; if(leaves[u] > num /2) return find(u); } return v; } int cnt = 0; void solve(){ dfs(1); for(int i=1; i<=n; i++) mark[i] = 0; int V = find(1); for(int i=1; i<=n; i++) mark[i] = 0; mark[V] = 1; for(int u: G[V]) make(u, cnt++); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; int u, v; for(int i=1; i<n; i++){ cin >> u >> v; G[u].push_back(v), G[v].push_back(u); } for(int i=1; i<=n; i++) if(G[i].size() == 1) num++; int res = (num+1) / 2; solve(); vector <int> ans((2 * res)); int pt = 0; for(int i=0; i<cnt; i++) for(int vi: s[i]){ ans[pt] = vi; pt += 2; if(pt == (2 * res)) pt = 1; } if((!ans.empty()) && ans.back() == 0) ans.back() = s[1][0]; cout << res << '\n'; for(int i=0; i<(int)ans.size(); i+=2) cout << ans[i] << ' ' << ans[i+1] << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...