이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include<fstream>
//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx2")
using namespace std;
ifstream fin("palpath.in");
ofstream fout("palpath.out");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ll mod = 1e9 + 7;
const int mxn = 5e5 + 5, mxr = 25e3, sq = 500, mxv = 1e6 + 5, mxvv = 130, pr = 31, inf = 1e9;
int n;
int in[mxn + 1];
vt<int>leaf;
vt<int>adj[mxn + 1];
void dfs(int s, int pre){
bool ok = 0;
for(auto i: adj[s]){
if(i != pre){
dfs(i, s); ok = 1;
}
}
if(!ok)leaf.pb(s);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
in[u]++; in[v]++;
adj[u].pb(v); adj[v].pb(u);
}
for(int i = 1; i <= n; i++){
if(in[i] > 1){
dfs(i, -1);
break;
}
}
cout << ((sz(leaf) + 1) / 2) << "\n";
for(int i = 0; i <= sz(leaf) / 2 - 1; i++){
cout << leaf[i] << " " << leaf[i + sz(leaf) / 2] << "\n";
}
if(sz(leaf) & 1){
cout << leaf[0] << " " << leaf.back();
}
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... |