답안 #76464

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
76464 2018-09-13T14:29:54 Z MrTEK Torrent (COI16_torrent) C++14
100 / 100
521 ms 23864 KB
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define len(a) (int)a.size()
#define fi first
#define sc second
#define d1(w) cerr<<#w<<":"<<w<<endl;
#define d2(w,c) cerr<<#w<<":"<<w<<" "<<#c<<":"<<c<<endl;
#define d3(w,c,z) cerr<<#w<<":"<<w<<" "<<#c<<":"<<c<<" "<<#z<<":"<<z<<endl;
#define left ind+ind
#define right ind+ind+1
#define mid (l+r)/2
#define FAST_IO ios_base::sync_with_stdio(false);
#define endl '\n'
#define bit __builtin_popcount

typedef long long int ll;

const int maxn = 620;
const long long LINF = 1e18;
const int LOG = 31;
const int INF = 1e9;
const int P = 31;
const int N = 3e5 + 5;
const int M = 1e6 + 5;
const int SQ = 350;
const int MOD = 1e9 + 7;

typedef long long int lli;
typedef pair<int,int> pii;

stack <pii> st;
vector <pii> ed[N],path;
int n,a,b,vis[N],vis2[N],id,h[N * 2],ans = INF;

int solve(int cur,int back) {
	vector <int> v;
	for (auto i : ed[cur])
		if (i.fi != back && h[i.sc] == 0)
			v.pb(solve(i.fi,cur));
	sort(v.begin(),v.end(),greater<int>());
	int re = 0,cnt = 0;
	for (auto i : v)	
		re = max(re,(++cnt) + i);
	return re;
}

bool dfs(int cur,int back,int back_edge) {
	bool flag = false;
	for (auto i : ed[cur]) {
		if (i.fi != back) {
			if (dfs(i.fi,cur,i.sc))
				flag = true;
		}
	}
	if (cur == b)
		flag = true;
	if (flag && cur != a)
		st.push(mp(cur,back_edge));
	return flag;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	// freopen(".gir","r",stdin);
	// freopen(".cik","w",stdout);

	cin >> n >> a >> b;
	for (int i = 1 ; i < n ; i++) {
		int u,v;
		cin >> u >> v;
		ed[u].pb(mp(v,++id));
		ed[v].pb(mp(u,++id));
	}
	dfs(a,-1,-1);
	while(len(st)) {
		path.pb(st.top());
		st.pop();
	}
	int l = 0, r = len(path) - 1;
	while(l <= r) {
		auto i = path[mid];
		if (i.sc % 2 == 0)
			h[i.sc] = h[i.sc - 1] = 1;
		else
			h[i.sc] = h[i.sc + 1] = 1;
		int ta = solve(a,-1);
		int tb = solve(b,-1);
		ans = min(ans,max(ta,tb));
		if (ta <= tb)
			l = mid + 1;
		else
			r = mid - 1;
		if (i.sc % 2 == 0)
			h[i.sc] = h[i.sc - 1] = 0;
		else
			h[i.sc] = h[i.sc + 1] = 0;
	}
	cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7568 KB Output is correct
3 Correct 9 ms 7640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 517 ms 20956 KB Output is correct
2 Correct 471 ms 22220 KB Output is correct
3 Correct 481 ms 23712 KB Output is correct
4 Correct 521 ms 23712 KB Output is correct
5 Correct 468 ms 23712 KB Output is correct
6 Correct 477 ms 23712 KB Output is correct
7 Correct 456 ms 23864 KB Output is correct