This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/** MIT License Copyright (c) 2018 Vasilyev Daniil **/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
template<typename T> using v = vector<T>;
#define int long long
typedef string str;
typedef vector<int> vint;
#define rep(a, l, r) for(int a = (l); a < (r); a++)
#define pb push_back
#define sz(a) ((int) a.size())
const long long inf = 4611686018427387903; //2^62 - 1
#if 0 //FileIO
const string fileName = "";
ifstream fin ((fileName == "" ? "input.txt" : fileName + ".in" ));
ofstream fout((fileName == "" ? "output.txt" : fileName + ".out"));
#define get fin>>
#define put fout<<
#else
#define get cin>>
#define put cout<<
#endif
#define eol put endl
void read() {} template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg) ;read(args...) ;}
void print(){} template<typename Arg,typename... Args> void print(Arg arg,Args... args){put (arg)<<" ";print(args...);}
void debug(){eol;} template<typename Arg,typename... Args> void debug(Arg arg,Args... args){put (arg)<<" ";debug(args...);}
int getInt(){int a; get a; return a;}
//code goes here
const int N = 500001;
vint g[N];
v<pair<int, int>> ans;
vint dfs(int cur, int p = -1) {
if (sz(g[cur]) == 1)
return vint({cur});
else {
vint pos;
for (int i : g[cur]) {
if (i == p)
continue;
vint z = dfs(i, cur);
if (sz(z) == 2 && sz(pos) == 1)
for (int j : z)
if (pos.empty())
pos.pb(j);
else {
ans.pb({pos.back(), j});
pos.pop_back();
}
else
for (int j : z)
if (sz(pos) < 2)
pos.pb(j);
else {
ans.pb({pos.back(), j});
pos.pop_back();
}
}
if (p == -1) {
if (sz(pos) == 2)
ans.pb({pos[0], pos[1]});
else if (g[pos[0]][0] != cur)
ans.pb({pos[0], cur});
else
rep(i, 1, N)
if (pos[0] != i && g[pos[0]][0] != i) {
ans.pb({pos[0], i});
break;
}
}
return pos;
}
}
void run() {
int n;
get n;
n++;
rep(i, 2, n) {
int v, u;
read(v, u);
g[v].pb(u);
g[u].pb(v);
}
rep(i, 1, n)
if (sz(g[i]) != 1) {
dfs(i);
break;
}
put sz(ans);
eol;
for (auto i : ans)
debug(i.first, i.second);
}
int32_t main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); put fixed; put setprecision(15); run(); 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... |