Submission #480849

#TimeUsernameProblemLanguageResultExecution timeMemory
480849PoPularPlusPlusNetwork (BOI15_net)C++17
0 / 100
8 ms12108 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007; 
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}

const int N = 500002;
int root;
vector<int> adj[N];
bool vis[N];
vector<pair<int,int>> v;
vector<int> y;

vector<int> dfs(int node){
	vis[node] = 1;
	if(adj[node].size() == 1){
		return {node};
	}
	vector<int> x,z;
	for(int i : adj[node]){
		if(!vis[i]){
			y.clear();
			y = dfs(i);
			x.pb(y[0]);
			if(y.size() == 2)z.pb(y[1]);
		}
	}
	for(int i : z)x.pb(i);
	z.clear();
	if(x.size() % 2 == 1){
		for(int i = 1; i < (int)x.size(); i+=2){
			v.pb(mp(x[i] , x[i + 1]));
		}
		if(node != root)return {x[0]};
		else {
			return {x[0] , x[1]};
		}
	}
	else {
		for(int i = 2; i < (int)x.size(); i+=2){
			v.pb(mp(x[i] , x[i + 1]));
		}
		return {x[0] , x[1]};
	}
}

void solve(){
	int n;
	cin >> n;
	for(int i = 1;  i< n; i++){
		int a , b;
		cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	for(int i = 1; i <= n; i++){
		if(adj[i].size() > 1)root = i;
	}
	vector<int> x = dfs(root);
	v.pb(mp(x[0] , x[1]));
	cout << v.size() << '\n';
	for(pair<int,int> p : v){
		cout << p.vf << ' ' << p.vs << '\n';
	}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
	//int t;cin >> t;while(t--)
	solve();
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...