因为从一个点出发还要回来,所以就是虚树边权的两倍
#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#define maxn 700021
#define inf 0x3fffffff
#define LL long long
using namespace std;
int h[maxn],n,m,f[maxn][21],cnt,sz,head[maxn],tot=1,dfn[maxn],cur[maxn],vis[maxn];
LL dis[maxn],ans;
set<int>s;
struct edge{int v,next;LL w;}e[maxn];
void adde(int a,int b,LL c){e[tot].v=b,e[tot].next=head[a],e[tot].w=c;head[a]=tot++;}
void dfs(int u,int fa){
h[u]=h[fa]+1;f[u][0]=fa;cur[dfn[u]=++cnt]=u;
for(int i=1;i<=20;i++)f[u][i]=f[f[u][i-1]][i-1];
for(int v,i=head[u];i;i=e[i].next){
if((v=e[i].v)==fa)continue;
dis[v]=dis[u]+e[i].w;
dfs(v,u);
}
}
int lca(int a,int b){
if(h[a]>h[b])swap(a,b);
for(int i=20;i>=0;i--)if(h[f[b][i]]>=h[a])b=f[b][i];
if(a==b)return a;
for(int i=20;i>=0;i--){
if(f[a][i]==f[b][i])continue;
a=f[a][i],b=f[b][i];
}return f[a][0];
}
LL Q(int a,int b){return dis[a]+dis[b]-dis[lca(a,b)]*2ll;}
void solve(){
int x,l,r;
while(m--){
scanf("%d",&x);
if(!vis[x]){//加入
l=*--s.lower_bound(dfn[x]),r=*s.upper_bound(dfn[x]);
if(l==-inf&&r!=inf)ans+=Q(x,cur[r]);
else if(l!=-inf&&r==inf)ans+=Q(x,cur[l]);
else if(l!=-inf&&r!=inf)ans+=2ll*min(Q(x,lca(cur[l],x)),Q(x,lca(cur[r],x)));
s.insert(dfn[x]); sz++;
}else{
l=*--s.lower_bound(dfn[x]),r=*s.upper_bound(dfn[x]);
if(l==-inf&&r!=inf)ans-=Q(x,cur[r]);
else if(l!=-inf&&r==inf)ans-=Q(x,cur[l]);
else if(l!=-inf&&r!=inf)ans-=2ll*min(Q(x,lca(cur[l],x)),Q(x,lca(cur[r],x)));
s.erase(dfn[x]);sz--;
}vis[x]^=1;
if(sz>1){
printf("%lld\n",ans+Q(cur[*++s.begin()],cur[*++s.rbegin()]));
}else printf("%lld\n",ans);
}
}
int main(){
s.insert(-inf),s.insert(inf);
scanf("%d%d",&n,&m);int a,b;LL c;
for(int i=1;i<n;i++){
scanf("%d%d%lld",&a,&b,&c);
adde(a,b,c),adde(b,a,c);
}dfs(1,0);
solve();
return 0;
}