1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ft first
#define sd second
#define mid ((l+r)>>1)
#define ls rt<<1
#define rs ls|1
const int N=2e5+5;
int n,m;
vector<pii> G[N];
vector<int> Ring;
int deg[N],a[N];
int sz[N],dep[N],fa[N],Rt[N],son[N],dfn[N],Id[N],tim,top[N];
int tr[N<<2];
struct edge
{
int u,v,w;
}E[N];
int val[N];
bool vis[N];
void dfs1(int u,int pa,int root)
{
dep[u]=dep[fa[u]=pa]+(sz[u]=1);
Rt[u]=root;
for(pii i:G[u]){
if(i.ft==pa||deg[i.ft]>0)
continue;
int v=i.ft;
a[v]=i.sd;
dfs1(v,u,root);
sz[u]+=sz[v];
if(sz[son[u]]<sz[v])
son[u]=v;
}
}
void dfs2(int u,int tp)
{
Id[dfn[u]=++tim]=u;
top[u]=tp;
if(!son[u])
return;
dfs2(son[u],tp);
for(pii i:G[u])
if(i.ft!=fa[u]&&i.ft!=son[u]&°[i.ft]<=0)
dfs2(i.ft,i.ft);
}
void pushup(int rt)
{
tr[rt]=tr[ls]+tr[rs];
}
void build(int rt,int l,int r)
{
if(l==r){
if(deg[Id[l]]>0)
return;
tr[rt]=a[Id[l]];
return;
}
build(ls,l,mid),build(rs,mid+1,r);
pushup(rt);
}
void update(int rt,int l,int r,int x,int k)
{
if(l==r){
tr[rt]=k;
return;
}
if(x<=mid)
update(ls,l,mid,x,k);
else
update(rs,mid+1,r,x,k);
pushup(rt);
}
int query(int rt,int l,int r,int lf,int rg)
{
if(lf>r||l>rg)
return 0;
if(lf<=l&&r<=rg)
return tr[rt];
return query(ls,l,mid,lf,rg)+query(rs,mid+1,r,lf,rg);
}
namespace R
{
int tr[N],Id[N],n;
void upd(int x,int k){
for(;x<=n;x+=x&-x){
tr[x]+=k;
}
}
int qry(int x,int k=0){
for(;x;x-=x&-x){
k+=tr[x];
}
return k;
}
void dfs3(int u,int tp)
{
if(vis[u])
return;
vis[u]=1;
for(pii i:G[u]){
if(deg[i.ft]<=0)
continue;
if(Id[i.ft]==0)
Id[i.ft]=Id[u]+1;
if(vis[i.ft]==0||i.ft==tp)
val[i.ft]=i.sd;
dfs3(i.ft,tp);
}
}
}
bool cmp(int x,int y)
{
return R::Id[x]<R::Id[y];
}
int Qry(int u,int v)
{
int res=0;
while(top[u]^top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
res+=query(1,1,n,dfn[top[u]],dfn[u]);
u=fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
res+=query(1,1,n,dfn[u]+1,dfn[v]);
return res;
}
signed main()
{
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++){
int u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
G[u].push_back({v,w});
G[v].push_back({u,w});
deg[u]++,deg[v]++;
E[i]={u,v,w};
}
queue<int> q;
for(int i=1;i<=n;i++)
if(deg[i]==1)
q.push(i),deg[i]=-1;
while(!q.empty()){
int u=q.front();q.pop();
for(pii i:G[u]){
if(deg[i.ft]==-1)
continue;
--deg[i.ft];
if(deg[i.ft]==1){
deg[i.ft]=-1;
q.push(i.ft);
}
}
}
for(int i=1;i<=n;i++)
if(deg[i]>0){
Ring.push_back(i);
dfs1(i,0,i);
dfs2(i,i);
}
build(1,1,n);
R::Id[Ring[0]]=1;
R::dfs3(Ring[0],Ring[0]);
sort(Ring.begin(),Ring.end(),cmp);
R::n=(int)Ring.size();
for(int i=0;i<R::n;i++)
R::upd(i+1,val[Ring[i]]);
while(m--){
int op,x,y,u,v;
scanf("%lld%lld%lld",&op,&x,&y);
if(op==1){
u=E[x].u,v=E[x].v;
if(deg[u]>0&°[v]>0){
int pos=max(R::Id[u],R::Id[v]);
if(min(R::Id[u],R::Id[v])==1&&pos==R::n)
pos=1;
int tmp=R::qry(pos)-R::qry(pos-1);
R::upd(pos,y-tmp);
}else{
int pos=(dep[u]<dep[v]?dfn[v]:dfn[u]);
update(1,1,n,pos,y);
}
}else{
if(deg[x]>0&°[y]>0){
if(R::Id[x]>R::Id[y])
swap(x,y);
int t1=R::qry(R::Id[y])-R::qry(R::Id[x]);
int t2=R::qry(R::Id[x])+R::qry(R::n)-R::qry(R::Id[y]);
printf("%lld\n",min(t1,t2));
}else{
if(Rt[x]^Rt[y]){
int res=Qry(Rt[x],x)+Qry(Rt[y],y);
if(R::Id[Rt[x]]>R::Id[Rt[y]])
swap(x,y);
int t1=R::qry(R::Id[Rt[y]])-R::qry(R::Id[Rt[x]]);
int t2=R::qry(R::Id[Rt[x]])+R::qry(R::n)-R::qry(R::Id[Rt[y]]);
printf("%lld\n",res+min(t1,t2));
}else{
printf("%lld\n",Qry(x,y));
}
}
}
}
return 0;
}
|