This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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... |