Submission #301495

# Submission time Handle Problem Language Result Execution time Memory
301495 2020-09-18T03:04:35 Z eggag32 Network (BOI15_net) C++17
0 / 100
3 ms 2688 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end() 
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 1e5 + 5;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

vi g[mxN];
int ch[mxN], p[mxN], repr[mxN], vis[mxN], cnt1[mxN];
int mx = 0, bst = -1;
int mx1 = 0, bst1 = -1;

void dfs(int cur, int prev, int d){
	if(d > mx){
		mx = d;
		bst = cur;
	}
	for(int x : g[cur]) if(x != prev) dfs(x, cur, d + 1);
}

void dfs1(int cur, int prev, int d){
	if(d > mx1){
		mx1 = d;
		bst1 = cur;
	}
	p[cur] = prev;
	for(int x : g[cur]) if(x != prev) dfs1(x, cur, d + 1);
}

vector<pi> fishyOrz;

void dfs2(int cur, int prev, int lst){
	int cnt = 0;
	if(ch[cur]) lst = cur;
	for(int x : g[cur]) if(x != prev) dfs2(x, cur, lst), cnt++;
	if(!cnt && !ch[cur]){
		assert(lst != -1);
		repr[lst] = cur;
		cnt1[lst]++;
		fishyOrz.pb({cur, lst});
	}
}

bool cmp(pi a, pi b){
	return a.se < b.se;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n;
	cin >> n;
	rep(i, n - 1){
		int a, b;
		cin >> a >> b;
		a--, b--;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(0, -1, 0);
	assert(bst != -1);
	dfs1(bst, -1, 0);
	assert(bst1 != -1);
	int nw = bst1;
	while(nw != -1){
		ch[nw] = 1;
		nw = p[nw];
	}
	dfs2(bst, -1, -1);
	vector<pi> ans;
	sort(all(fishyOrz), cmp);
	int l = 0, r = (int)fishyOrz.size() - 1;
	while(l <= r){
		if(l == r){
			int nd = -1;
			for(int x : g[fishyOrz[l].se]) if(ch[x]) nd = x;
			assert(nd != -1);
			ans.pb({fishyOrz[l].fi, nd});
		}
		else{
			ans.pb({fishyOrz[l].fi, fishyOrz[r].fi});
			if(fishyOrz[l].se != fishyOrz[r].se){
				vis[fishyOrz[l].se] = vis[fishyOrz[r].se] = 1;
			}
		}
		l++, r--;
	}
	rep(i, n){
		int nd = -1;
		int nm1 = 0;
		for(int x : g[i]) if(!ch[x]) nd = x, nm1++;
		int nd1 = -1;
		for(int x : g[i]) if(ch[x]) nd1 = x;
		if(ch[i] && (nd != -1) && !vis[i] && (nd1 != -1) && cnt1[i] % 2 == 0 && nm1 == 1) ans.pb({repr[i], nd1});
	}
	ans.pb({bst, bst1});
	cout << (int)ans.size() << '\n';
	for(pi x : ans) cout << x.fi + 1 << " " << x.se + 1 << '\n';
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Incorrect 2 ms 2688 KB Invalid number of links.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Incorrect 2 ms 2688 KB Invalid number of links.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Incorrect 2 ms 2688 KB Invalid number of links.
3 Halted 0 ms 0 KB -