이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// khodaya khodet komak kon
// Toxicity
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define lc id << 1
# define rc id << 1 | 1
# define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 5e5 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const int mod = 998244353;
const int base = 257;
int n, sz[xn], root, m, a[xn];
vector <int> adj[xn];
void plant(int v, int p = - 1){
sz[v] = 1;
for (int u : adj[v])
if (u != p)
plant(u, v), sz[v] += sz[u];
}
int find_centroid(int v, int p = - 1){
for (int u : adj[v])
if (u != p && sz[u] + sz[u] > n)
return find_centroid(u, v);
return v;
}
void DFS(int v, int p = - 1){
if (SZ(adj[v]) == 1)
a[m ++] = v;
for (int u : adj[v])
if (u != p)
DFS(u, v);
}
int main(){
InTheNameOfGod;
cin >> n;
for (int i = 0; i < n - 1; ++ i){
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
plant(1);
root = find_centroid(1);
DFS(root);
cout << (m + 1) / 2 << endl;
for (int i = 0; i < (m + 1) / 2; ++ i)
cout << a[i] << sep << a[i + m / 2] << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |