1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<cstring>
5 #include<cmath>
6 #include<vector>
7 #include<set>
8 #define N (100010)
9 #define inf (0x7f7f7f7f)
10 #define index IIIIII
11 #define LL long long
12 using namespace std;
13 set <int> S;
14 typedef set<int>::iterator iter;
15 int dep[N],f[N][18],bh[N]; LL d[N][18],c[N<<2];
16 int fi[N<<2],ne[N<<2],to[N<<2],b[N],dfn[N],n,m,R,P,rt,edge,index,cnt;
17 bool vis[N],vv[N]; LL ans;
18 inline int read()
19 {
20 int data=0;char ch=0;
21 while(ch<‘0‘ || ch>‘9‘) ch=getchar();
22 while(ch>=‘0‘ && ch<=‘9‘) data=data*10+ch-‘0‘,ch=getchar();
23 return data;
24 }
25
26 void add(int x,int y,int z)
27 {
28 to[++edge]=y,ne[edge]=fi[x],fi[x]=edge,c[edge]=z;
29 }
30 void dfs1(int u,int Fa)
31 {
32 f[u][0]=Fa;
33 bh[++index]=u,dfn[u]=index;
34 for(int i=fi[u];i;i=ne[i])
35 {
36 int v=to[i];
37 if(v!=Fa)dep[v]=dep[u]+1,d[v][0]=c[i],dfs1(v,u);
38 }
39 }
40 void bz()
41 {
42 for(int j=1;j<=17;j++)
43 for(int i=1;i<=n;i++)
44 f[i][j]=f[f[i][j-1]][j-1],d[i][j]=d[f[i][j-1]][j-1]+d[i][j-1];
45 }
46 void addans(int x,int y,LL tp)
47 {
48 if(dep[x]<dep[y])swap(x,y);
49 int k=dep[x]-dep[y];
50 for(int i=0;i<=17;i++)
51 if(k&(1<<i))ans+=(LL)d[x][i]*tp,x=f[x][i];
52 if(x==y)return;
53 for(int i=17;i>=0;i--)
54 if(f[x][i]!=f[y][i])ans+=(d[x][i]+d[y][i])*tp,x=f[x][i],y=f[y][i];
55 ans+=(d[x][0]+d[y][0])*tp;
56 }
57 iter pre(iter t){return (t==S.begin())?--S.end():--t;}
58 iter suc(iter t){return (t==--S.end())?S.begin():++t;}
59 void Add(int x)
60 {
61 if(S.size())
62 {
63 iter t=S.lower_bound(dfn[x]),t1;
64 if(t==S.end())t=S.begin(); t1=pre(t);
65 addans(x,bh[*t],1),addans(x,bh[*t1],1),addans(bh[*t],bh[*t1],-1);
66 }
67 S.insert(dfn[x]);
68 }
69 void Del(int x)
70 {
71 iter t=S.find(dfn[x]);
72 iter tl=pre(t),tr=suc(t);
73 addans(bh[*tl],x,-1),addans(x,bh[*tr],-1),addans(bh[*tl],bh[*tr],1);
74 S.erase(dfn[x]);
75 }
76 int main()
77 {
78 //freopen("stone.in","r",stdin);
79 //freopen("stone.out","w",stdout);
80 n=read(),m=read();
81 for(int i=1;i<n;i++)
82 {
83 int x=read(),y=read(),z=read();
84 add(x,y,z),add(y,x,z);
85 }
86 dfs1(1,0),bz();
87 while(m--)
88 {
89 int x=read();
90 vv[x]^=1; char ch;
91 if(vv[x])ch=‘+‘;else ch=‘-‘;
92 if(ch==‘+‘)Add(x);
93 if(ch==‘-‘)Del(x);
94 printf("%lld\n",ans);
95 }
96 }