This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second
vector<vi> adj(500005);
vi leaves;
void dfs(int x, int p){
bool has_children = 0;
for(auto i : adj[x]){
if(i != p){
dfs(i, x);
has_children = 1;
}
}
if(!has_children)
leaves.pb(x);
}
void solve(){
int n;
cin >> n;
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
if(adj[1].size() == 1)
leaves.pb(1);
dfs(1, -1);
cout << ceil((double)leaves.size()/2) << '\n';
for(int i = 0; i < leaves.size()/2; i++)
cout << leaves[i] << ' ' << leaves[i + leaves.size()/2] << '\n';
if(leaves.size() & 1)
cout << leaves[0] << ' ' << leaves.back() << '\n';
}
int main(){
//freopen("flota.in", "r", stdin);
//freopen("flota.out", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
Compilation message (stderr)
net.cpp: In function 'void solve()':
net.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < leaves.size()/2; i++)
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |