Submission #915026

#TimeUsernameProblemLanguageResultExecution timeMemory
915026penguin133Network (BOI15_net)C++17
100 / 100
673 ms92880 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
vector <int> adj[500005];
vector <int> stuf[500005];
vector <pi> ans;

void dfs(int x, int par){
	bool f = 0;
	for(auto i : adj[x]){
		if(i != par){
			f = 1;
			dfs(i, x);
		}
	}
	if(!f)stuf[x].push_back(x);
	else{
		for(auto i : adj[x]){
			while((int)stuf[x].size() + (int)stuf[i].size() >= 3){
				ans.push_back({stuf[x].back(), stuf[i].back()});
				stuf[x].pop_back(); stuf[i].pop_back();
			}
			for(auto j : stuf[i])stuf[x].push_back(j);
		}
	}
	if(x == 1){
		if((int)adj[x].size() <= 1)stuf[x].push_back(x);
		while(stuf[x].size() >= 2){
			int tmp = stuf[x].back(); stuf[x].pop_back();
			int tmp2 = stuf[x].back(); stuf[x].pop_back();
			ans.push_back({tmp, tmp2});
		}
		if(!stuf[x].empty())ans.push_back({stuf[x].back(), (stuf[x].back() == 1 ? n : 1)});
	}
}

void solve(){
	cin >> n;
	for(int i=1;i<n;i++)
	{
		int a, b; cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, -1);
	cout << (int)ans.size() << '\n';
	for(auto [i, j] : ans)cout << i << ' ' << j << '\n';
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message (stderr)

net.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   61 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...