This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#ifdef wiwihorz
#define print(a...) cerr << "Line " << __LINE__ << ": ", kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L; }
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#else
#define print(...) 0
#define vprint(...) 0
#endif
namespace solver {
int n, root, timestamp, lg;
vector<vector<int>> mp, ac;
vector<int> vis, d, cur;
vector<pii> ans;
void init_(int _n) {
n = _n, root = -1, timestamp = 0;
lg = 31 - __builtin_clz(n);
mp.assign(n + 1, vector<int>());
ac.assign(lg + 1, vector<int>(n + 1, 0));
vis.assign(n + 1, 0);
d.assign(n + 1, 0);
}
void dfs(int x, int par, int dd) {
ac[0][x] = par, d[x] = dd;
vis[x] = ++timestamp;
int cnt = 0;
for(auto i : mp[x]) if(i != par) {
cnt ++, dfs(i, x, dd + 1);
}
if(!cnt) cur.push_back(x);
}
int LCA(int a, int b) {
if(d[a] < d[b]) swap(a, b);
int need = d[a] - d[b];
rep(i, 0, lg) if((need >> i) & 1)
a = ac[i][a];
if(a == b) return a;
rrep(i, 0, lg) if(ac[i][a] != ac[i][b])
a = ac[i][a], b = ac[i][b];
return ac[0][a];
}
void solve() {
rep(i, 1, n) if(mp[i].size() > 1) root = i;
dfs(root, root, 0);
rep(i, 1, lg) rep(j, 1, n) {
ac[i][j] = ac[i - 1][ac[i - 1][j]];
}
while(cur.size() > 1) {
vector<int> next;
int h = cur.size() / 2, sz = cur.size();
sort(all(cur), [](int a, int b) {
return vis[a] < vis[b];
});
cur.resize(unique(all(cur)) - cur.begin());
// print(root, cur.size());
// vprint(all(cur));
for(int i = 0; i < h; i ++) {
int lca = LCA(cur[i], cur[i + h + (sz & 1)]);
ans.push_back({cur[i], cur[i + h + (sz & 1)]});
}
if(sz & 1) next.push_back(cur[h]);
cur = next;
}
if(cur.size()) ans.push_back({cur[0], root});
cout << ans.size() << "\n";
for(auto i : ans) cout << i.first << " " << i.second << "\n";
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n; cin >> n;
init_(n);
rep(i, 1, n - 1) {
int a, b; cin >> a >> b;
mp[a].push_back(b);
mp[b].push_back(a);
}
solve();
return 0;
}
Compilation message (stderr)
net.cpp:4: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
4 | #pragma loop-opt(on)
|
net.cpp:24:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
24 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L; }
| ^~~~
net.cpp:24:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
24 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L; }
| ^~~~
net.cpp: In function 'void solver::solve()':
net.cpp:79:9: warning: unused variable 'lca' [-Wunused-variable]
79 | int lca = LCA(cur[i], cur[i + h + (sz & 1)]);
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |