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>
#pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 3e3+10;
const int INF = 1e18+10;
const int LOG = 19;
const int MOD = 1e9+7;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
int n;
vector <int> adj[MAXN];
vector <pii> ans;
vector <int> leaf;
int root;
void dfs(int nw, int par){
if(adj[nw].size()==1) leaf.pb(nw);
for(auto nx : adj[nw]){
if(nx==par) continue;
dfs(nx, nw);
}
}
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i=1; i<=n-1; i++){
int x, y; cin >> x >> y;
adj[x].pb(y); adj[y].pb(x);
}
for(int i=1; i<=n; i++){
if(adj[i].size()!=1) root = i;
}
dfs(root, 0);
for(int i=0; i<leaf.size()/2; i++) ans.pb({leaf[i], leaf[leaf.size()-i-1]});
if(leaf.size()%2){
ans.pb({leaf[leaf.size()/2], leaf[0]});
}
cout << ans.size() << '\n';
for(auto in : ans) cout << in.fi << ' ' << in.se << '\n';
}
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:50:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i=0; i<leaf.size()/2; i++) ans.pb({leaf[i], leaf[leaf.size()-i-1]});
| ~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |