This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
const int N = 2e5 + 5, MOD = 1e9 + 7;
int n, tin[N], T;
vector<int> adj[N];
void dfs(int v, int p) {
tin[v] = ++T;
for (auto u : adj[v])
if (u != p)
dfs(u, v);
}
void solve() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int v, u; cin >> v >> u;
adj[v].pb(u), adj[u].pb(v);
}
int root = 1;
for (int i = 1; i <= n; i++) {
if (adj[i].size() >= 2)
root = i;
}
dfs(root, 0);
vector<pair<int, int> > vec, ans;
for (int i = 1; i <= n; i++) {
if (adj[i].size() == 1)
vec.pb(mp(tin[i], i));
}
sort(vec.begin(), vec.end());
if (vec.size() % 2) {
ans.pb(mp(vec.back().se, root));
vec.pop_back();
}
for (int i = 0; i < vec.size() / 2; i++)
ans.pb(mp(vec[i].se, vec[i + vec.size() / 2].se));
cout << ans.size() << '\n';
for (auto [v, u] : ans)
cout << v << ' ' << u << '\n';
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
Compilation message (stderr)
net.cpp: In function 'void solve()':
net.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < vec.size() / 2; i++)
| ~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |