Submission #1270620

#TimeUsernameProblemLanguageResultExecution timeMemory
1270620cowwycowNetwork (BOI15_net)C++20
100 / 100
187 ms39052 KiB
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define name "aaaaaa"
#define fi first
#define se second
#define endl "\n"
using ll = long long;
using db = double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdb = pair<db, db>;
using ppii = pair<int, pii>;
using ull = unsigned long long;
using vvi = vector<vector<ll>>;

void file(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	if(fopen(name".inp", "r")) {
		freopen(name".inp", "r", stdin);
		freopen(name".out", "w", stdout);
	}
}

const int N = 5e5 + 5;

vector<int> e[N];

int timer = 1;
int tin[N];

void dfs(int u, int par){
	tin[u] = timer++;
	for(int v : e[u]){
		if(v == par) continue;
		dfs(v, u);
	}
}

void solve(){
	int n; cin >> n;
	for(int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		e[u].push_back(v);
		e[v].push_back(u);
	}

	dfs(1, -1);

	vector<pii> leaf;
	for(int i = 1; i <= n; i++){
		if(e[i].size() == 1){
			leaf.push_back({tin[i], i});
		}
	}
	sort(leaf.begin(), leaf.end());

	cout << (leaf.size() + 1) / 2 << endl;
	for(int i = 0; i < (leaf.size() + 1) / 2; i++){
		cout << leaf[i].se << ' ' << leaf[i + leaf.size() / 2].se << endl;
	}
}

int main(){
	file();
	int test = 1;
	//cin >> test;
	while(test--){
		solve();
	}
}

Compilation message (stderr)

net.cpp: In function 'void file()':
net.cpp:20:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |                 freopen(name".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:21:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |                 freopen(name".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...