#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int n,m,p;
int odu[100010]={0},go[100010][2]={{0}};
bool oduch[100010]={0},check[100010]={0};
vector< pair<int,int> > l[100010];
int queue[100010][2]={{0}};
int start,end;
int ans[100010]={0},acnt=0;
void f1(void){
int i,j;
for(i=0;i<m;i++){
for(j=1;j<=n;j++) check[j] = false;
queue[0][0] = odu[i], queue[0][1] = 0;
check[odu[i]] = true;
for(start=0,end=1;start<end;start++){
for(j=0;j<(int)l[queue[start][0]].size();j++){
if(!check[l[queue[start][0]][j].first]){
check[l[queue[start][0]][j].first] = true;
queue[end][0] = l[queue[start][0]][j].first;
queue[end][1] = queue[start][1] + l[queue[start][0]][j].second;
if(oduch[queue[end][0]]){
ans[acnt++] = queue[end][1];
}
end++;
}
}
}
}
sort(ans,ans+acnt);
printf("%d\n",ans[2*(p-1)]);
}
void f2(void){
}
int sum[100010]={0};
int cnt(int val){
int i,start,end,mid,ans=0;
for(i=0;i<m;i++){
start = i, end = m-1;
while(start<end){
mid = (start+end+1)/2;
if(sum[mid]-sum[i] > val) end = mid - 1;
else start = mid;
}
ans += start - i;
}
return ans;
}
void f3(void){
int i,j;
for(i=0,j=1;i<m;i++){
if(i) sum[i] = sum[i-1];
else sum[i] = 0;
for(;j<=odu[i];j++) sum[i] += go[j][1];
}
int start,end,mid,t;
start = 0, end = sum[m-1];
while(start<end){
mid = (start+end)/2;
t = cnt(mid);
if(t < p) start = mid+1;
else end = mid;
}
printf("%d\n",start);
}
int main()
{
int a,b,i;
scanf("%d%d%d",&n,&m,&p);
for(i=2;i<=n;i++){
scanf("%d%d",&a,&b);
go[i][0] = a,go[i][1] = b;
l[i].push_back( make_pair(a,b) );
l[a].push_back( make_pair(i,b) );
}
for(i=0;i<m;i++){
scanf("%d",&odu[i]);
oduch[odu[i]] = true;
}
if(n<=100) f1();
else{
if(p==1) f2();
else f3();
}
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 |
0 ms |
6484 KB |
Output is correct |
6 |
Correct |
0 ms |
6484 KB |
Output is correct |
7 |
Correct |
0 ms |
6484 KB |
Output is correct |
8 |
Correct |
0 ms |
6484 KB |
Output is correct |
9 |
Correct |
0 ms |
6484 KB |
Output is correct |
10 |
Correct |
0 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6484 KB |
Output is correct |
2 |
Correct |
0 ms |
6616 KB |
Output is correct |
3 |
Correct |
0 ms |
6616 KB |
Output is correct |
4 |
Correct |
12 ms |
7012 KB |
Output is correct |
5 |
Correct |
32 ms |
7936 KB |
Output is correct |
6 |
Correct |
116 ms |
8728 KB |
Output is correct |
7 |
Correct |
76 ms |
9124 KB |
Output is correct |
8 |
Incorrect |
156 ms |
9520 KB |
Output isn't correct |
9 |
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 |
0 ms |
6484 KB |
Output is correct |
7 |
Correct |
0 ms |
6484 KB |
Output is correct |
8 |
Correct |
0 ms |
6484 KB |
Output is correct |
9 |
Correct |
0 ms |
6484 KB |
Output is correct |
10 |
Correct |
0 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
6480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
6484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |