Submission #394649

# Submission time Handle Problem Language Result Execution time Memory
394649 2021-04-27T06:43:58 Z saniyar_krmi Network (BOI15_net) C++14
0 / 100
16 ms 24296 KB
// Possible Overdose for this coder
#include <bits/stdc++.h>
#define put(x) cerr << #x << ": " << x << '\n'
#define line() cerr << "**************\n"

//#define F first
//#define S second
//#define mul(x, y) (((x) * (y)) %mod)
//#define bit(i, j) (((i)>>(j)) &1)
//#define left(id) ((id<<1) + 1)
//#define right(id) ((id<<1) + 2)
 
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;

const int maxn = 5e5 + 10;
int n;
vector <int> G[maxn];
bool mark[maxn];
int leaves[maxn], num;

void dfs(int v){
	mark[v] = 1;
	if(G[v].size() == 1)
		leaves[v] = 1;
	for(int u: G[v]){
		if(mark[u])
			continue;
		dfs(u);
		leaves[v] += leaves[u];
	}
}

vector <int> s[maxn];
void make(int v, int x){
	mark[v] = 1;
	if(G[v].size() == 1)
		s[x].push_back(v);

	for(int u: G[v]){
		if(mark[u])
			continue;
		make(u, x);
	}
}

int find(int v){
	mark[v] = 1;

	for(int u: G[v]){
		if(mark[u])
			continue;
		if(leaves[u] > num /2)
			return find(u);
	}

	return v;
}

int cnt = 0;
void solve(int root){
	dfs(root);
	num = leaves[root];

	fill(mark, mark+maxn, 0);
	int V = find(root);

	fill(mark, mark+maxn, 0);
	mark[V] = 1;
	for(int u: G[V])
		make(u, cnt++);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);  cout.tie(0);

	cin >> n;
	if(n == 1)
		return cout << "0\n", 0;
	if(n == 2)
		return cout << "1\n1 2\n", 0;

	int u, v;
	for(int i=1; i<n; i++){
		cin >> u >> v;
		G[u].push_back(v), G[v].push_back(u);
	}
	int root = 1;
	for(int v=1; v<=n; v++)
		if(G[v].size() > 1)
			root = v;
	solve(root);

	int res = (num+1) / 2;

	vector <int> ans(2 * res);
	int pt = 0;
	for(int i=0; i<cnt; i++)
		for(int vi: s[i]){
			ans[pt] = vi;
			pt += 2;
			if(pt == 2 * res)
				pt = 1;
		}
	
	if((!ans.empty()) && ans.back() == 0)
		ans.back() = s[1][0];
	
	cout << res << '\n';
	for(int i=0; i<(int)ans.size(); i+=2)
		cout << ans[i] << ' ' << ans[i+1] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24268 KB Output is correct
2 Correct 15 ms 24260 KB Output is correct
3 Correct 15 ms 24296 KB Output is correct
4 Incorrect 15 ms 24268 KB Breaking single line is causing network to disconnect.
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24268 KB Output is correct
2 Correct 15 ms 24260 KB Output is correct
3 Correct 15 ms 24296 KB Output is correct
4 Incorrect 15 ms 24268 KB Breaking single line is causing network to disconnect.
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24268 KB Output is correct
2 Correct 15 ms 24260 KB Output is correct
3 Correct 15 ms 24296 KB Output is correct
4 Incorrect 15 ms 24268 KB Breaking single line is causing network to disconnect.
5 Halted 0 ms 0 KB -