이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2")
#pragma GCC optimize("unroll-loops,O3")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 1e5+20,mod = 1e9+7,inf = 1e9+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
vector<int> adj[N];
int tin[N],T;
bool cmp(int u,int v){
return (tin[u] < tin[v]);
}
void dfs(int v,int p){
tin[v] = T++;
for(int u : adj[v]) if (u != p) dfs(u,v);
}
int main(){
ios_base :: sync_with_stdio(0); cin.tie(0);
int n,ans = 0;
cin >> n;
rep(i,1,n){
int u,v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1,0);
vector<int> ve;
rep(i,1,n+1) if (adj[i].size() == 1){
ans++;
ve.pb(i);
}
cout << (ans+1)/2 << endl;
sort(all(ve),cmp);
rep(i,0,ans){
if (i == ans-i-1){
cout << ve[i] << ' ' << ve[0] << endl;
continue;
}
if (i > ans-i-1) break;
cout << ve[i] << ' ' << ve[ans-i-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... |