답안 #27444

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
27444 2017-07-12T15:34:16 Z gs14004 Torrent (COI16_torrent) C++14
100 / 100
246 ms 33640 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;

vector<int> gph[300005];
int n, a, b;
int par[300005], chk[300005], dp[300005];

void dfs(int x, int p){
	for(auto &i : gph[x]){
		if(i != p){
			par[i] = x;
			dfs(i, x);
		}
	}
}

void getdp(int x, int p){
	vector<int> v;
	for(auto &i : gph[x]){
		if(i != p){
			getdp(i, x);
			v.push_back(dp[i]);
		}
	}
	sort(v.begin(), v.end());
	for(int i=0; i<v.size(); i++){
		dp[x] = max(dp[x], v[i] + (int)v.size() - i);
	}
}

vector<int> dpl[300005];

int solve(int x, int l, int p){
	if(l < 0) return 0;
	int nxt = -1;
	for(auto &i : gph[x]){
		if(i == p) continue;
		if(chk[i]) nxt = i;
	}
	int val = l - 1;
	for(int i=0; i<dpl[x].size(); i++){
		if(l - i - 1 < dpl[x][i]){
			return 0;
		}
		if(l - i - 1 == dpl[x][i]){
			val = l - i - 2;
		}
	}
	if(nxt == -1) return 1;
	return solve(nxt, val, x) + 1;
}

int main(){
	int len = 0;
	cin >> n >> a >> b;
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d",&s,&e);
		gph[s].push_back(e);
		gph[e].push_back(s);
	}
	dfs(a, -1);
	for(int i=b; i; i=par[i]){
		chk[i] = 1;
		len++;
	}
	for(int i=b; i; i=par[i]){
		for(auto &j : gph[i]){
			if(!chk[j]){
				getdp(j, i);
				dpl[i].push_back(dp[j]);
			}
		}
		sort(dpl[i].begin(), dpl[i].end());
		reverse(dpl[i].begin(), dpl[i].end());
	}
	int s = 0, e = n;
	while(s != e){
		int m = (s+e)/2;
		if(solve(a, m, -1) + solve(b, m, -1) >= len) e = m;
		else s = m+1;
	}
	cout << s;
}

Compilation message

torrent.cpp: In function 'void getdp(int, int)':
torrent.cpp:28:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<v.size(); i++){
                ^
torrent.cpp: In function 'int solve(int, int, int)':
torrent.cpp:43:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<dpl[x].size(); i++){
                ^
torrent.cpp: In function 'int main()':
torrent.cpp:60:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&s,&e);
                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19600 KB Output is correct
2 Correct 6 ms 19600 KB Output is correct
3 Correct 6 ms 19600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 31144 KB Output is correct
2 Correct 183 ms 31952 KB Output is correct
3 Correct 199 ms 32892 KB Output is correct
4 Correct 193 ms 32944 KB Output is correct
5 Correct 193 ms 30624 KB Output is correct
6 Correct 186 ms 31488 KB Output is correct
7 Correct 246 ms 33640 KB Output is correct