Submission #394674

#TimeUsernameProblemLanguageResultExecution timeMemory
394674ali_tavakoliNetwork (BOI15_net)C++14
100 / 100
508 ms45300 KiB
//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
	
typedef long long ll;
#define pb push_back
#define F first
#define S second
//#pragma GCC optimize("Ofast")
	
const int maxn = 5e5 + 5;
	
int n, st;
vector<int> adj[maxn], leaf;
	
void dfs(int v, int p = 0)
{
	if(adj[v].size() == 1)
		leaf.pb(v);
	for(auto u : adj[v])
		if(u != p)
			dfs(u, v);
}
	
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i = 0; i < n - 1; 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)
		{
			dfs(i);
			st = i;
			break;
		}
	
	cout << (leaf.size() + 1) / 2 << '\n';
	int last;
	//cout << leaf.size() << '\n';
	//return 0;
	
	for(int i = 0; i + leaf.size() / 2 < leaf.size(); i ++)
	{
		cout << leaf[i] << " " << leaf[i + leaf.size() / 2] << '\n';
		last = i + leaf.size() / 2;
	}

	if(last != leaf.size() - 1)
		cout << leaf[leaf.size() - 1] << " " << st << '\n';
}
	
/*
6
1 2
2 3
2 4
5 4
6 4
	
8
1 2
2 3
3 4
4 5
3 6
3 7
3 8
*/

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:56:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |  if(last != leaf.size() - 1)
      |     ~~~~~^~~~~~~~~~~~~~~~~~
net.cpp:56:5: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
   56 |  if(last != leaf.size() - 1)
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...