답안 #26282

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
26282 2017-06-29T03:43:13 Z kdh9949 Torrent (COI16_torrent) C++14
100 / 100
246 ms 31424 KB
#include <bits/stdc++.h>
using namespace std;

int n, a, b, isp[300010], cnt;
vector<int> e[300010], pth, v[300010];

int dfs(int x, int p){
	isp[x] = 1;
	if(x == b){
		pth.push_back(x);
		return 1;
	}
	for(auto &i : e[x]){
		if(i == p) continue;
		if(dfs(i, x)){
			pth.push_back(x);
			return 1;
		}
	}
	isp[x] = 0;
	return 0;
}

int f(int x, int p){
	vector<int> v;
	for(auto &i : e[x]){
		if(i == p) continue;
		v.push_back(f(i, x));
	}
	sort(v.begin(), v.end(), greater<int>());
	int ret = 0, c = 0;
	for(auto &i : v){
		ret = max(ret, i + ++c);
	}
	return ret;
}

int pos(int k, int s, int d){
    int t = 0;
    for(; s >= 1 && s <= cnt; s += d){
        int nt = t + 1;
        for(auto &i : v[s]){
			int lt = k - i - ++t;
			if(lt < 0) return s;
			if(lt == 0) nt = t + 1;
        }
		t = nt;
    }
    return s;
}

bool can(int k){
	return pos(k, 1, 1) > pos(k, cnt, -1);
}

int main(){
	scanf("%d%d%d", &n, &a, &b);
	for(int i = 0; i < n - 1; i++){
		int x, y; scanf("%d%d", &x, &y);
		e[x].push_back(y);
		e[y].push_back(x);
	}
	dfs(a, 0);
	for(auto &i : pth){
		cnt++;
		for(auto &j : e[i]){
			if(isp[j]) continue;
			v[cnt].push_back(f(j, i));
		}
		sort(v[cnt].begin(), v[cnt].end(), greater<int>());
	}
	int s = 0, e = n;
	while(s <= e){
		int m = (s + e) / 2;
		if(can(m)) e = m - 1;
		else s = m + 1;
	}
	printf("%d\n", s);
}

Compilation message

torrent.cpp: In function 'int main()':
torrent.cpp:57:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &n, &a, &b);
                             ^
torrent.cpp:59:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x, y; scanf("%d%d", &x, &y);
                                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17256 KB Output is correct
2 Correct 3 ms 17256 KB Output is correct
3 Correct 3 ms 17256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 176 ms 28988 KB Output is correct
2 Correct 246 ms 30072 KB Output is correct
3 Correct 186 ms 30808 KB Output is correct
4 Correct 153 ms 31336 KB Output is correct
5 Correct 176 ms 28496 KB Output is correct
6 Correct 173 ms 29280 KB Output is correct
7 Correct 166 ms 31424 KB Output is correct