이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 500005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
ll n, r = 0;
pair<ll, ll> dp[MAXN];
vector<ll> adj[MAXN];
vector<pair<ll, ll>> cn[MAXN];
void dfs(ll v, ll p){
if(adj[v].size() == 1 && adj[v][0] == p){
r++, dp[v] = {1, v};
return;
}
dp[v] = {2, -1};
for(ll v2 : adj[v]) if(p != v2){
dfs(v2, v);
if(dp[v].first == 2){
swap(cn[v], cn[v2]), dp[v] = dp[v2];
continue;
}
if(dp[v].first == 0){
if(dp[v2].first == 0){
pair<ll, ll> bk = cn[v].back(), bk2 = cn[v2].back();
cn[v].pop_back(), cn[v2].pop_back();
cn[v].push_back({bk.first, bk2.first});
cn[v2].push_back({bk.second, bk2.second});
}else{
pair<ll, ll> bk = cn[v].back();
cn[v].pop_back();
cn[v].push_back({bk.first, dp[v2].second});
dp[v] = {1, bk.second};
}
}else{
if(dp[v2].first == 0){
pair<ll, ll> bk = cn[v2].back();
cn[v2].pop_back();
cn[v2].push_back({bk.first, dp[v].second});
dp[v].second = bk.second;
}else{
cn[v].push_back({dp[v].second, dp[v2].second});
dp[v].first = 0;
}
}
if(cn[v].size() < cn[v2].size()) swap(cn[v], cn[v2]);
}
}
int main(){
cin >> n;
FOR(i, 1, n - 1){
ll a, b; cin >> a >> b;
adj[a].push_back(b), adj[b].push_back(a);
}
dfs(1, -1);
r = (r + 1) / 2;
bool x1 = (adj[1].size() == 1 && dp[1].first == 0);
FOR(i, 1, n){
for(pair<ll, ll> pi : cn[i]){
if(x1) {
cout << pi.first << " " << 1 << endl;
cout << pi.second << " " << 1 << endl;
x1 = false;
continue;
}
cout << pi.first << " " << pi.second << endl;
}
}
if(dp[1].first) cout << dp[1].second << " " << 1 << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |