Submission #1127053

#TimeUsernameProblemLanguageResultExecution timeMemory
1127053AgageldiNetwork (BOI15_net)C++20
0 / 100
9 ms14400 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define N 600005
#define ff first
#define ss second
#define pb push_back
#define sz(s) (int)s.size()
#define rep(c, a, b) for(c = a; c <= b; c++)
 
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
ll n, t = -1, out[N], mx, nd, p, o[N];
vector <int> v[N], answer;
vector <pair<int,int>> ans;
map <int,int> vis, vip;

void solve(int x,int jog, int cnt) {
	vis[x] = 1;
	for(auto i:v[x]) {
		if(vis[i]) continue;
		solve(i,jog,cnt + 1);
	}
	if(o[x] == 1 && vip[x] == 0 && mx < cnt) {
		mx = cnt;
		nd = x;
	}
}
int main () {
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> n;
	for(int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}
	for(int i = 1;i<=n;i++) {
		if(sz(v[i]) == 1) {
			answer.pb(i);
			o[i] = 1;
			p++;
		}
	}
	if(p%2) {
		t = answer[sz(answer) - 1];
		o[t] = 0;
		answer.pop_back();
	}
	for(auto i : answer) {
		if(vip[i]) continue;
		vis.clear();
		mx = nd = 0;
		vip[i] = 1;
		solve(i, i, 0);
		vip[nd] = 1;
		cout << i << " " << nd << '\n';
	}
	if(p % 2 == 0) return 0;
	cout << t << " " << answer[0] << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...