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;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
int n;
vi G[500100], leaf;
bool vis[500100];
void dfs(int at, int p){
bool made= false;
vis[at] = true;
for(auto next : G[at]){
if(!vis[next]){
made = true;
dfs(next, at);
}
}
if(!made){
leaf.pb(at);
}
}
int main(){
cin>>n;
for(int i = 1; i < n; i++){
int a, b;
cin>>a>>b;
a--; b--;
G[a].pb(b);
G[b].pb(a);
}
memset(vis, false, sizeof vis);
dfs(0, -1);
// vis[0] = true;
vector<pii> ans;
for(int i = 0; i < leaf.size()-1; i+=2){
ans.pb(mp(leaf[i]+1, leaf[i+1]+1));
}
ans.pb(mp(leaf[leaf.size()-1], 1));
cout<<ans.size()<<endl;
for(auto t : ans)
cout<<t.first<<" "<<t.second<<endl;
return 0;
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < leaf.size()-1; 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... |