# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1270637 | BuiDucManh123 | Network (BOI15_net) | C++20 | 233 ms | 41036 KiB |
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define taskname ""
#define ld long double
const int mod = 1e9+7;
using namespace std;
vector<int> g[500009];
int tin[500009];
int timer = 0;
void dfs(int u, int p){
tin[u] = ++timer;
for(int v : g[u]){
if(v == p) continue;
dfs(v, u);
}
}
bool cmp(int x, int y){
return tin[x] < tin[y];
}
signed main() {
if (fopen(taskname".inp","r")) {
freopen(taskname".inp","r",stdin);
freopen(taskname".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
int root = 0;
vector<int> order;
for(int i = 1; i <= n; i++){
if(g[i].size() > 1){
root = i;
}
}
for(int i = 1; i <= n; i++){
if(g[i].size() == 1){
order.pb(i);
}
}
//cout << root << "\n";
dfs(root, root);
sort(order.begin(), order.end(), cmp);
cout << (order.size() + 1) / 2 << "\n";
int sz = order.size();
for(int i = 0; i < sz / 2; i++){
cout << order[i] << " " << order[i + sz / 2] << "\n";
}
if(sz & 1){
cout << order.back() << " " << order[0] << "\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... |