1 #include <iostream>
2 #include <iomanip>
3 #include <cstdlib>
4 #include <cstdio>
5 #include <cmath>
6 #include <string>
7 #include <cstring>
8 #include <algorithm>
9 #include <set>
10 #define ll long long
11 #define RG register
12 const int N = 100050;
13
14 using namespace std;
15
16 set<int>s;
17 set<int> :: iterator it,qi,ho;
18
19 int gi(){
20 char ch=getchar();int x=0;
21 while(ch<‘0‘ || ch>‘9‘) ch=getchar();
22 while(ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
23 return x;
24 }
25
26 int cnt,nn[N*2][3],head[N],dis[N],fa[N],son[N],siz[N],top[N],vis[N],id[N],ra[N];
27 long long ans,g[N];
28
29 void dfs1(int x,int f){
30 dis[x]=dis[f]+1,fa[x]=f;
31 for (RG int i=head[x]; i; i=nn[i][0])
32 if (nn[i][1]!=f){
33 g[nn[i][1]]=g[x]+nn[i][2],dfs1(nn[i][1],x);
34 if (siz[nn[i][1]]>siz[son[x]]) son[x]=nn[i][1];
35 siz[x]+=siz[nn[i][1]];
36 }
37 ++siz[x];
38 return;
39 }
40
41 void dfs2(int x,int tp){
42 top[x]=tp,id[x]=++cnt,ra[cnt]=x;
43 if (son[x]) dfs2(son[x],tp);
44 for (RG int i=head[x]; i; i=nn[i][0])
45 if (nn[i][1]!=son[x] && nn[i][1]!=fa[x])
46 dfs2(nn[i][1],nn[i][1]);
47 return;
48 }
49
50 int lca(int a,int b){
51 while(top[a]!=top[b])
52 if (dis[top[a]]>dis[top[b]]) a=fa[top[a]];
53 else b=fa[top[b]];
54 if (dis[a]>dis[b]) swap(a,b);
55 return a;
56 }
57
58 ll query(int x){
59 it=s.find(id[x]);
60 if (it==s.begin()) {qi=s.end();qi--;}
61 else {qi=it;qi--;}
62 ho=it;ho++;
63 if (ho==s.end()) ho=s.begin();
64 return g[ra[*ho]]+g[ra[*it]]*2+g[ra[*qi]]-g[lca(ra[*ho],ra[*it])]*2-g[lca(ra[*it],ra[*qi])]*2;
65 }
66
67 ll work(int x){
68 it=s.find(id[x]);
69 if (it==s.begin()) {qi=s.end();qi--;}
70 else {qi=it;qi--;}
71 ho=it;ho++;
72 if (ho==s.end()) ho=s.begin();
73 return g[ra[*ho]]+g[ra[*qi]]-2*g[lca(ra[*qi],ra[*ho])];
74 }
75
76 int main(){
77 int n=gi(),m=gi();
78 for (RG int i=1; i<n; ++i){
79 int l=gi(),r=gi(),s=gi();
80 nn[++cnt][1]=l,nn[cnt][0]=head[r],head[r]=cnt,nn[cnt][2]=s;
81 nn[++cnt][1]=r,nn[cnt][0]=head[l],head[l]=cnt,nn[cnt][2]=s;
82 }
83 cnt=0,dfs1(1,0),dfs2(1,1);
84 for (RG int i=1; i<=m; ++i){
85 int x=gi();
86 if (vis[x]){
87 ans-=query(x);
88 ans+=work(x);
89 s.erase(id[x]),vis[x]=0;
90 }
91 else{
92 vis[x]=1;
93 s.insert(id[x]);
94 ans-=work(x);
95 ans+=query(x);
96 }
97 printf("%lld\n",ans);
98 }
99 return 0;
100 }