Submission #639709

#TimeUsernameProblemLanguageResultExecution timeMemory
639709WunkaNetwork (BOI15_net)C++17
0 / 100
8 ms14048 KiB
#include<bits/stdc++.h> using namespace std; const int N = 5e5 + 5; vector<vector<int>> graph(N); vector<bool> visited(N); vector<int> degree(N); map<int, int> leaf; int k = 0, mx, x; void dfs(int v) { visited[v] = true; if(degree[v] == 1) { leaf[k] = v; k++; } for(int next : graph[v]) { if(!visited[next]) { dfs(next); } } } int main() { int n; cin >> n; mx = 0; for(int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--, b--; degree[a]++, degree[b]++; graph[a].push_back(b); graph[b].push_back(a); if(degree[a] > mx) { mx = degree[a]; x = a; } if(degree[b] > mx) { mx = degree[b]; x = b; } } dfs(x); cout << (k + 1) / 2 << '\n'; /* if(k % 2 == 1) { cout << x + 1 << ' ' << leaf[k] + 1 << '\n'; k--; } */ for(int i = 0; i < k - i - 1; i++) { cout << leaf[i] + 1 << ' ' << leaf[k - i - 1] + 1 << '\n'; } if(k % 2 == 1) { cout << leaf[k / 2] + 1 << ' ' << x + 1 << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...