This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author win11905
*/
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define iii tuple<int, int, int>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 5e5+5;
class net {
private:
int n;
vector<int> g[N], ans;
void dfs(int u, int p) {
if(g[u].size() == 1) ans.emplace_back(u);
for(int v : g[u]) if(v != p) dfs(v, u);
}
public:
void solve(istream& cin, ostream& cout) {
cin >> n;
int snode;
for(int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].emplace_back(v), g[v].emplace_back(u);
}
for(int i = 1; i <= n; ++i) if(g[i].size() > 1) snode = i;
dfs(snode, 0);
int m = ((int)ans.size() + 1) >> 1;
cout << m << endl;
if(ans.size() % 2) cout << snode << ' ' << ans[m-1] << endl;
for(int i = 0; i+m < (int)ans.size(); ++i) cout << ans[i] << ' ' << ans[i+m] << endl;
}
};
class Solver {
public:
void solve(std::istream& in, std::ostream& out) {
net *obj = new net();
obj->solve(in, out);
}
};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solver solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
Compilation message (stderr)
net.cpp: In member function 'void net::solve(std::istream&, std::ostream&)':
net.cpp:34:13: warning: 'snode' may be used uninitialized in this function [-Wmaybe-uninitialized]
int snode;
^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |