답안 #6035

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
6035 2014-06-15T19:15:22 Z ainta 오두막집 (GA7_cabana) C++
0 / 100
360 ms 24880 KB
#pragma warning(disable:4996)
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>E[100010], L[100010];
vector<int>P[100010];
struct Seg{
	int L, num;
	bool operator < (const Seg &p)const{
		return L < p.L;
	}
};
vector<Seg>w[100010];
int n, m, D[100010], L2[100010], Q[100010], par[100010], sz;
bool chk[100010], House[100010];
long long K;
void BFS(int a){
	int h = 0, t = 0, i, x;
	Q[++t] = a, D[a] = 1, L2[a] = 0;
	while (h < t){
		x = Q[++h];
		for (i = 0; i < E[x].size(); i++){
			if (!D[E[x][i]] && !chk[E[x][i]]){
				D[E[x][i]] = 1;
				Q[++t] = E[x][i];
				L2[E[x][i]] = L2[x] + L[x][i];
				par[E[x][i]] = x;
			}
		}
	}
	sz = t;
}
int GetMid(int a){
	int i, x;
	BFS(a);
	for (i = sz; i >= 1; i--){
		x = Q[i];
		D[par[x]] += D[x];
		if (D[x] >= sz / 2){
			break;
		}
	}
	for (i = 1; i <= sz; i++)D[Q[i]] = 0;
	return x;
}
void Do(int a)
{
	int Mid = GetMid(a);
	if (sz == 1){
		if (House[a])P[a].push_back(0);
		return;
	}
	int i, x, j;
	Seg tp;
	chk[Mid] = true;
	for (i = 0; i < E[Mid].size(); i++){
		x = E[Mid][i];
		if (!chk[x]){
			Do(x);
			for (j = 0; j < P[x].size(); j++){
				tp.L = P[x][j] + L[Mid][i];
				tp.num = x;
				w[Mid].push_back(tp);
			}
			P[x].resize(0);
		}
	}
	if (House[Mid]){
		tp.L = 0, tp.num = Mid;
		w[Mid].push_back(tp);
	}
	if (!w[Mid].empty()){
		sort(w[Mid].begin(), w[Mid].end());
	}
	chk[Mid] = false;
	BFS(a);
	for (i = 1; i <= sz; i++){
		D[Q[i]] = 0;
		if (House[Q[i]])P[a].push_back(L2[Q[i]]);
	}
}
int cnt[100010];
long long Gap(int Len)
{
	int i, pv, j, sz;
	long long S = 0;
	for (i = 1; i <= n; i++){
		if (w[i].empty())continue;
		pv = -1;
		sz = w[i].size();
		for (j = sz - 1; j >= 0; j--){
			while (pv + 1 < sz && w[i][pv + 1].L + w[i][j].L <= Len){
				pv++;
				cnt[w[i][pv].num]++;
			}
			S += pv + 1 - cnt[w[i][j].num];
		}
		for (j = 0; j <= pv; j++)cnt[w[i][j].num]--;
	}
	return S;
}
int main(){
	int i, a, b, be, ed, mid, Res;
	scanf("%d%d%lld", &n, &m, &K);
	for (i = 2; i <= n; i++){
		scanf("%d%d", &a, &b);
		E[a].push_back(i);
		E[i].push_back(a);
		L[a].push_back(b);
		L[i].push_back(b);
	}
	for (i = 0; i < m; i++){
		scanf("%d", &a);
		House[a] = true;
	}
	Do(1);
	be = 1, ed = 250 * (n - 1);
	while (be <= ed){
		mid = (be + ed) >> 1;
		if (Gap(mid) >= K * 2){
			Res = mid;
			ed = mid - 1;
		}
		else{
			be = mid + 1;
		}
	}
	printf("%d\n", Res);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 12736 KB Output is correct
2 Incorrect 0 ms 12736 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 12736 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 12736 KB Output is correct
2 Correct 0 ms 12736 KB Output is correct
3 Incorrect 0 ms 12736 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 12736 KB Output is correct
2 Correct 4 ms 13000 KB Output is correct
3 Correct 20 ms 14056 KB Output is correct
4 Correct 176 ms 19204 KB Output is correct
5 Correct 72 ms 15904 KB Output is correct
6 Correct 128 ms 17620 KB Output is correct
7 Correct 164 ms 19072 KB Output is correct
8 Correct 228 ms 21448 KB Output is correct
9 Correct 192 ms 19204 KB Output is correct
10 Correct 240 ms 21316 KB Output is correct
11 Correct 4 ms 13264 KB Output is correct
12 Correct 196 ms 19204 KB Output is correct
13 Correct 360 ms 24624 KB Output is correct
14 Correct 200 ms 19204 KB Output is correct
15 Correct 344 ms 24880 KB Output is correct
16 Incorrect 96 ms 18940 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 12736 KB Output isn't correct
2 Halted 0 ms 0 KB -