我不排序军队到根的距离居然AC了。。。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node{
int x;
ll ss;
};
int n,m,a,b,cnt,to[100050],nxt[100050],h[50050],ffa[50050];
ll f[50050][50],tme[50050][50],s[50050];
ll sum,c[100050],w;
bool p[50050],bb[100050];
ll dh[50050],idx,dhh[50050],far[50050];
node k[50050];
bool cmp(node a,node b){return a.ss>b.ss;}
void add(int x,int y,ll z){
to[++cnt]=y;
c[cnt]=z;
nxt[cnt]=h[x];
h[x]=cnt;
}
void d(int x,int fa,int fa1){
ffa[x]=fa1;
int l=1;
while(f[f[x][l-1]][l-1]!=0){
f[x][l]=f[f[x][l-1]][l-1];
tme[x][l]=tme[x][l-1]+tme[f[x][l-1]][l-1];
l++;
}
for(int i=h[x];i;i=nxt[i])
if(to[i]!=fa){
tme[to[i]][0]=c[i];
f[to[i]][0]=x;
far[to[i]]=far[x]+c[i];
d(to[i],x,fa1);
}
}
bool dfs(int x,int fa){
if(p[x]==1) return 0;
if(s[x]==1) return 1;
for(int i=h[x];i;i=nxt[i])
if(to[i]!=fa)
if(dfs(to[i],x)==1) return 1;
return 0;
}
int bz(ll t,int x){
int l=x;ll sm=0;
for(int i=40;i>=0;i--)
while(sm+tme[l][i]<=t){
if(f[l][i]==1){
if(t-(sm+tme[l][i])<=c[ffa[l]])
if(dfs(to[ffa[l]],1)==1)
return to[ffa[l]];
dh[++idx]=t-(sm+tme[l][i]);
return 0;
}
sm+=tme[l][i],l=f[l][i];
}
return l;
}
bool ch(){
int ct=0,idx2=1;
sort(dh+1,dh+idx+1);
for(int i=h[1];i;i=nxt[i]){
if(dfs(to[i],1)) dhh[++ct]=c[i];
}
sort(dhh+1,dhh+ct+1);
if(ct==0||ct==0&&idx==0) return true;
for(int i=1;i<=idx;i++){
if(dh[i]>=dhh[idx2]) idx2++;
if(idx2==ct+1) return true;
}
return false;
}
int ff(){
int l=0,r=sum,mid;
while(l<=r){
mid=(l+r)/2;
for(int i=1;i<=m;i++) p[bz(mid,k[i].x)]=1;
if(ch()==true) r=mid-1;
else l=mid+1;
for(int i=1;i<=n;i++) p[i]=0;
idx=0;
}
return l;
}
int main(){
memset(tme,-245,sizeof(tme));
cin>>n;
for(int i=1;i<n;i++){
cin>>a>>b>>w;
add(a,b,w);add(b,a,w);
s[a]++,s[b]++,sum+=w;;
}
for(int i=h[1];i;i=nxt[i]){
f[to[i]][0]=1;
tme[to[i]][0]=c[i];
d(to[i],1,i);
}
cin>>m;
for(int i=1;i<=m;i++) cin>>k[i].x,k[i].ss=far[k[i].x];
// sort(k+1,k+m+1,cmp);
cout<<ff();
return 0;
}