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 unsigned long long ull;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 500007;
const ll inf = 1e18;
const ll mod = 1e9+7;
const double pi = acos(-1);
const int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
const int block = 320;
int n;
vector <int> v, adj[M];
vector < pair <int,int> > ans;
void dfs(int node, int p, int src){
vector <int> cur;
for(auto i : adj[node]) if(i != p && adj[i].size() > 1) {
dfs(i, node, src);
if(node == src){
if(cur.size()){
ans.pb({cur.back(), v.back()});
cur.pop_back();
v.pop_back();
}
for(auto j : v) cur.pb(j);
v.clear();
}
}
for(auto i : adj[node]) if(i != p && adj[i].size() == 1) {
dfs(i, node, src);
if(node == src){
if(cur.size()){
ans.pb({cur.back(), v.back()});
cur.pop_back();
v.pop_back();
}
for(auto j : v) cur.pb(j);
v.clear();
}
}
if(src == node){
for(int i = 0; i < (int)cur.size() - 1; i += 2) ans.pb({cur[i], cur[i + 1]});
if(cur.size() % 2) ans.pb({cur.back(), src});
}
if(adj[node].size() == 1) v.pb(node);
return;
}
int main(){
cin >> n;
for(int i = 1; i < n; ++i){
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i = 1; i <= n; ++i) if(adj[i].size() > 1) {dfs(i, 0, i); break;}
cout << ans.size() << endl;
for(auto i : ans) cout << i.F << " " << i.S << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |