Submission #394615

#TimeUsernameProblemLanguageResultExecution timeMemory
394615woldis4Network (BOI15_net)C++17
100 / 100
526 ms69076 KiB
# include <bits/stdc++.h>

/*
// ordered_set 
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
*/

using namespace std;

typedef long long                                        ll;
typedef long double                                      ld;
typedef pair <int, int>                                  pii;
typedef pair <pii, int>                                  ppi;
typedef pair <int, pii>                                  pip;
typedef pair <pii, pii>                                  ppp;
typedef pair <ll, ll>                                    pll;

# define A                                               first
# define B                                               second
# define endl                                            '\n'
# define sep                                             ' '
# define all(x)                                          x.begin(), x.end()
# define kill(x)                                         return cout << x << endl, 0
# define SZ(x)                                           int(x.size())
# define lc                                              id << 1
# define rc                                              id << 1 | 1
# define InTheNameOfGod                                  ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
#define MP make_pair

ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}

const int xn = 1e6 + 10;
const int xm = 1e2 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const int mod = 998244353;//1e9 + 7;//998244353;
const int base = 257;

ll n, m, k, st[xn], num[xn], cnt = 1;
vector <int> adj[xn];

void dfs(int v, int par = 0){
	num[cnt] = v;
	st[v] = cnt++;
	for (int u : adj[v]){
		if (u != par){
			dfs(u, v);
		}
	}
}

int main(){
	InTheNameOfGod;
    cin >> n;
    for (int i = 0; i < n-1; ++i){
    	int v, u;
    	cin >> v >> u;
    	adj[v].push_back(u);
    	adj[u].push_back(v);
	}
	int mx = 0, indmx = -1;
	for (int i = 1; i <= n; ++i){
		if (int(adj[i].size()) > mx)	indmx = i, mx = int(adj[i].size());
	}
	dfs(indmx);
	vector <int> vec;
	for (int i = 1; i <= n; ++i){
		if (adj[num[i]].size() == 1)	vec.push_back(num[i]);
	}
	int sz = int(vec.size());
	cout << (sz+1)/2 << '\n';
	if (sz%2 == 1)	cout << vec[0] << ' ' << vec[sz-1] << '\n';
	for (int i = 0; i < sz/2; ++i)	cout << vec[i] << ' ' << vec[sz/2+i] << '\n';
    return 0;
}

/*
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
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...