This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |