Submission #849582

# Submission time Handle Problem Language Result Execution time Memory
849582 2023-09-15T03:37:14 Z dungz Network (BOI15_net) C++17
0 / 100
3 ms 12276 KB
//#pragma GCC optimize ("O2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define task "task"
#define task "task"
#define prll pair<ll,ll>
#define pb push_back
#define ld long double
const ll MIN=-1e18,MAX=1e18,MOD=1e9+7;
vector<int> a[500005];
vector<int> leaves;
void dfs(int u,int par)
{
	for(auto i:a[u])
	{
		if(i!=par)
		{
			if(a[i].size()==1) leaves.push_back(i);
			else dfs(i,u);
		}
	}
}
int main(){
   	//freopen (task".inp", "r", stdin);
   	//freopen (task".out", "w", stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	dfs(1,0);
//	for(auto i:leaves) cout<<i<<" ";
//	cout<<endl;
	if(leaves.size()%2==0)
	{
		cout<<leaves.size()/2<<endl;
		for(int i=0;i<=leaves.size()/2-1;i++)
		{
			cout<<leaves[i]<<" "<<leaves[i+leaves.size()/2]<<endl;
		}
	}
	else
	{
		cout<<leaves.size()/2+1<<endl;
		for(int i=0;i<=leaves.size()/2-1;i++)
		{
			cout<<leaves[i]<<" "<<leaves[i+leaves.size()/2+1]<<endl;
		}
		cout<<leaves[leaves.size()/2]<<" "<<1<<endl;
	}
}
/*

*/

Compilation message

net.cpp: In function 'int main()':
net.cpp:46:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |   for(int i=0;i<=leaves.size()/2-1;i++)
      |               ~^~~~~~~~~~~~~~~~~~~
net.cpp:54:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for(int i=0;i<=leaves.size()/2-1;i++)
      |               ~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12120 KB Output is correct
2 Correct 3 ms 12276 KB Output is correct
3 Correct 3 ms 12124 KB Output is correct
4 Correct 3 ms 12124 KB Output is correct
5 Correct 3 ms 12120 KB Output is correct
6 Correct 3 ms 12124 KB Output is correct
7 Incorrect 3 ms 12124 KB Invalid number of links.
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12120 KB Output is correct
2 Correct 3 ms 12276 KB Output is correct
3 Correct 3 ms 12124 KB Output is correct
4 Correct 3 ms 12124 KB Output is correct
5 Correct 3 ms 12120 KB Output is correct
6 Correct 3 ms 12124 KB Output is correct
7 Incorrect 3 ms 12124 KB Invalid number of links.
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12120 KB Output is correct
2 Correct 3 ms 12276 KB Output is correct
3 Correct 3 ms 12124 KB Output is correct
4 Correct 3 ms 12124 KB Output is correct
5 Correct 3 ms 12120 KB Output is correct
6 Correct 3 ms 12124 KB Output is correct
7 Incorrect 3 ms 12124 KB Invalid number of links.
8 Halted 0 ms 0 KB -