Submission #1270622

#TimeUsernameProblemLanguageResultExecution timeMemory
1270622khoianhNetwork (BOI15_net)C++20
100 / 100
194 ms58952 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mn = 5e5 + 5;
ll n, a[mn], root;
vector<ll> v[mn], u;


void dfs(ll i, ll parent){
	if(parent && (int)v[i].size() == 1){
		u.push_back(i);
		return;
	}
	for(auto j : v[i]){
		if(j == parent) continue;
		dfs(j, i);
	}
	return;
}

void solve(){
	cin >> n;
	for(int i = 1; i < n; ++i){
		int x, y;
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for(int i = 1; i <= n; ++i){
		if(v[i].size() != 1) root = i;
	}
	dfs(root, 0);
	cout << ((int)u.size() + 1) / 2 << "\n";
	ll t = ((int)u.size() + 1) / 2;
	for(int i = 0; i < t; ++i){
//		if(i == 0) cout << u[i] << " " << root << "\n";
//		else 
		cout << u[i] << " " << u[(i + t) % (int)u.size()] << "\n";
	}
//	if(t * 2 != (int)u.size()) cout << u.back() << " " << root << "\n";
    return;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	if(fopen(".INP", "r")) {
		freopen(".INP", "r", stdin);
		freopen(".OUT", "w", stdout);
	}
	int testCase = 1;
	//cin >> testCase;
	while(testCase--) solve();
}

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:48:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |                 freopen(".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~
net.cpp:49:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |                 freopen(".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...