제출 #639710

#제출 시각아이디문제언어결과실행 시간메모리
639710WunkaNetwork (BOI15_net)C++17
0 / 100
7 ms14036 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 << endl; /* 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 << endl; } if(k % 2 == 1) { cout << leaf[k / 2] + 1 << ' ' << x + 1 << endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...