# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1270579 | nathan4690 | Network (BOI15_net) | C++20 | 328 ms | 41924 KiB |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name "TEST"
using namespace std;
const ll maxn = 1e6+5, inf=1e18;
int n, sp[maxn], timer = 1;
vector<int> G[maxn], all;
bool cmp(int u, int v){
return sp[u] < sp[v];
}
void dfs(int u, int p){
sp[u] = timer++;
for(int c: G[u]){
if(c != p){
dfs(c, u);
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n;
f1(i,n-1){
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
if(n == 2){
cout << "1\n1 2\n";
return 0;
}
int root = 0;
f1(i,n) {
if(G[i].size() == 1) all.push_back(i);
else root = i;
}
dfs(root, 0);
sort(all.begin(), all.end(), cmp);
cout << (all.size() + 1) / 2 << '\n';
for(int i=0;i+i<all.size();i++){
int j = i + all.size() / 2;
cout << all[i] << ' ' << all[j] << '\n';
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |