Submission #95942

#TimeUsernameProblemLanguageResultExecution timeMemory
95942luciocfNetwork (BOI15_net)C++14
100 / 100
677 ms60424 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5+10; typedef pair<int, int> pii; int leaf[maxn], tt; vector<int> grafo[maxn]; void dfs(int u, int p) { for (auto v: grafo[u]) { if (v == p) continue; dfs(v, u); } if (grafo[u].size() == 1) leaf[++tt] = u; } int main(void) { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; grafo[u].push_back(v); grafo[v].push_back(u); } int root; for (int i = 1; i <= n; i++) if (grafo[i].size() > 1) root = i; dfs(root, 0); int ans = (tt+1)/2; cout << ans << "\n"; for (int i = 1; i < ans; i++) cout << leaf[i] << " " << leaf[i+ans] << "\n"; if (tt&1) cout << leaf[ans] << " " << leaf[1] << "\n"; else cout << leaf[ans] << " " << leaf[tt] << "\n"; }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:43:5: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
  dfs(root, 0);
  ~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...