#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
template<typename T> bool chmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
const int MAXN = 500010;
int n;
int cnt = 1;
int in[MAXN];
int pre[MAXN];
vector<int> g[MAXN];
priority_queue<pi, vector<pi>, greater<pi> > mn;
priority_queue<pi> mx;
int done[MAXN];
vector<pi> ans;
void dfs(int x, int p) {
pre[x] = cnt++;
for (auto i: g[x]) {
if (i == p) continue;
dfs(i, x);
}
if (in[x] == 1) {
mn.push({pre[x], x});
mx.push({pre[x], x});
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
++in[u], ++in[v];
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 1);
int temp = mx.top().si;
while (mx.size()) {
pi u = mx.top();
pi v = mn.top();
if (done[u.si] || done[v.si]) break;
if (u.si == v.si) {
ans.pb({u.si, temp});
break;
}
else ans.pb({u.si, v.si});
mx.pop(); mn.pop();
done[u.si] = 1; done[v.si] = 1;
}
cout << (int)ans.size() << '\n';
for (auto i: ans) cout << i.fi << ' ' << i.si << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Incorrect |
6 ms |
11988 KB |
Breaking single line is causing network to disconnect. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Incorrect |
6 ms |
11988 KB |
Breaking single line is causing network to disconnect. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Incorrect |
6 ms |
11988 KB |
Breaking single line is causing network to disconnect. |
3 |
Halted |
0 ms |
0 KB |
- |