This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
const int maxn = 500100;
vector<int> adj[maxn], lef;
void dfs(int u, int par){
int cnt = 0;
for(int v : adj[u]) if(v - par){
cnt++, dfs(v, u);
}
if(!cnt) lef.push_back(u);
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
int n; cin>>n;
for(int i=0; i<n-1; i++) {
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
vector<int> a = lef;
lef.clear();
dfs(2, 0);
vector<int> b = lef;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
vector<int> c;
merge(a.begin(), a.end(),
b.begin(), b.end(),
back_inserter(c));
c.erase(unique(c.begin(), c.end()), c.end());
if(c.size() == 1) {
cout<<1<<endl;
cout<<c[0]<<" " << 1<<endl;
return 0;
}
cout<<(int) (c.size() + 1)/2<<endl;
for(int i=0; i+1<c.size(); i+=2) {
cout<<c[i]<<" " << c[i+1]<<endl;
}
if(c.size() & 1) {
cout << c[0] << " " << c[c.size()-1]<<endl;
}
}
Compilation message (stderr)
net.cpp: In function 'int main(int, const char**)':
net.cpp:45:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i+1<c.size(); i+=2) {
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |