#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 5e5+5;
vector<int> adj[N], lf;
void dfs(int x=1, int pr=-1){
tr(it, adj[x])
if(*it!=pr)
dfs(*it, x);
if((int)adj[x].size()==1)
lf.pb(x);
}
int main(){
int n;
scanf("%d", &n);
for(int i = 1; i < n; ++i){
int a, b;
scanf("%d%d", &a, &b);
adj[a].pb(b);
adj[b].pb(a);
}
dfs();
vector<pii> ans;
for(int i = 0; i < ((int)lf.size()+1)/2; ++i)
ans.pb({lf[i], lf[i+(int)lf.size()/2]});
printf("%d\n", (int)ans.size());
tr(it, ans)
printf("%d %d\n", it->ff, it->ss);
return 0;
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
net.cpp:31:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |