답안 #6049

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
6049 2014-06-15T20:55:01 Z kriii 오두막집 (GA7_cabana) C++
19 / 100
6000 ms 19840 KB
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

int N,M; long long K;
int x[100001],y[100001],c[100001],chk[100001]; bool fb[100001],ca[100001];
vector<int> g[100001];
int q[100001],z[100001],d[100001];

pair<vector<int>, long long> over(int s, int dist)
{
	int n = 0, h = 0;

	q[h] = s; d[h] = 0; h++; chk[s] = s;
	while (n < h){
		int u = q[n], w = d[n]; n++;

		for (int t=0,i;t<g[u].size();t++) if (!fb[i=g[u][t]]){
			int v = (u == x[i]) ? y[i] : x[i];
			if (chk[v] != s){
				q[h] = v; d[h] = (w+c[i]); h++; chk[v] = s;
			}
		}
	}

	long long opp = 0; int cut = -1;
	for (int i=n-1;i>=0;i--){
		int u = q[i]; z[i] = 1;
		for (int t=0,j;t<g[u].size();t++) if (!fb[j=g[u][t]]){
			int v = (u == x[j]) ? y[j] : x[j];
			if (chk[v] != s){
				z[i] += z[-chk[v]];
				if (opp < (long long) n * (n - z[-chk[v]])){
					opp = (long long) n * (n - z[-chk[v]]);
					cut = j;
				}
			}
		}
		chk[u] = -i;
	}

	pair<vector<int>, long long> ret;
	for (int i=0;i<n;i++) if (ca[q[i]]) ret.first.push_back(d[i]);
	sort(ret.first.begin(),ret.first.end());

	ret.second = 0;
	if (cut != -1){
		fb[cut] = 1;
		pair<vector<int>, long long> a = over(x[cut],dist);
		pair<vector<int>, long long> b = over(y[cut],dist);
		ret.second = a.second + b.second;
		for (int i=0,j=(int)b.first.size()-1;i<a.first.size();i++){
			while (j >= 0 && a.first[i] + c[cut] + b.first[j] > dist) j--;
			ret.second += j + 1;
		}
		fb[cut] = 0;
	}

	return ret;
}

int main()
{
	scanf ("%d %d %lld",&N,&M,&K);
	for (int i=2;i<=N;i++){
		scanf ("%d %d",&y[i],&c[i]); x[i] = i;
		g[x[i]].push_back(i);
		g[y[i]].push_back(i);
	}
	while (M--){
		int v; scanf ("%d",&v);
		ca[v] = 1;
	}

	int l = 1, r = 250 * N, m, a;
	while (l <= r){
		m = (l + r) / 2;
		if (over(1,m).second >= K){
			r = m - 1;
			a = m;
		}
		else l = m + 1;
	}

	printf ("%d\n",a);

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 6484 KB Output is correct
2 Correct 0 ms 6484 KB Output is correct
3 Correct 0 ms 6484 KB Output is correct
4 Correct 0 ms 6484 KB Output is correct
5 Correct 4 ms 6484 KB Output is correct
6 Correct 4 ms 6484 KB Output is correct
7 Correct 4 ms 6484 KB Output is correct
8 Correct 4 ms 6484 KB Output is correct
9 Correct 0 ms 6484 KB Output is correct
10 Correct 4 ms 6484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 344 ms 8904 KB Output is correct
2 Execution timed out 6000 ms 19312 KB Program timed out
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 6484 KB Output is correct
2 Correct 0 ms 6484 KB Output is correct
3 Correct 0 ms 6484 KB Output is correct
4 Correct 0 ms 6484 KB Output is correct
5 Correct 0 ms 6484 KB Output is correct
6 Correct 4 ms 6484 KB Output is correct
7 Correct 0 ms 6484 KB Output is correct
8 Correct 4 ms 6484 KB Output is correct
9 Correct 4 ms 6484 KB Output is correct
10 Correct 8 ms 6484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 440 ms 7192 KB Output is correct
2 Execution timed out 6000 ms 18520 KB Program timed out
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 744 ms 8868 KB Output is correct
2 Execution timed out 6000 ms 19840 KB Program timed out
3 Halted 0 ms 0 KB -