链接:http://acm.hdu.edu.cn/showproblem.php?pid=6393
思路:n个点,n条边,也就是基环树。。因为只有一个环,我们可以把这个环断开,建一个新的点n+1与之相连,然后就按照树链剖分求边权的方法分类讨论下,过不过这条被分开的边,一共有三种情况取值最小的。
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define mid int m = (l + r) >> 1
const int M = 1e5+;
int cnt,cnt1,head[M],fa[M],dep[M],son[M],siz[M],top[M],tid[M],n,q,vis[M];
ll val[M];
ll sum[M<<];
struct node{
int to,next;
ll val;
}e[M]; struct node1{
int u,v;
ll val;
}a[M];
void add(int u,int v){
e[++cnt].to = v;e[cnt].next = head[u];head[u] = cnt;
} void dfs1(int u,int faz,int deep){
dep[u] = deep;
fa[u] = faz;
siz[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v == fa[u]) continue;
dfs1(v,u,deep+);
siz[u] += siz[v];
if(siz[v] > siz[son[u]]||son[u]==-)
son[u] = v;
}
} void dfs2(int u,int t){
top[u] = t;
tid[u] = ++cnt1;
if(son[u] == -) return ;
dfs2(son[u],t);
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa[u]&&v != son[u])
dfs2(v,v); }
} void pushup(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void build(int l,int r,int rt){
if(l == r){
sum[rt] = val[l];
return ;
}
mid;
build(lson); build(rson);
pushup(rt);
} void update(int p,ll c,int l,int r,int rt){
if(l == r){
sum[rt] = c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
} ll query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r) return sum[rt];
mid;
ll ret = ;
if(L <= m) ret += query(L,R,lson);
if(R > m) ret += query(L,R,rson);
return ret;
} /*void ct(int l,int r,int rt){
if(l == r){
cout<<sum[rt]<<" ";
return ;
}
mid;
ct(lson); ct(rson);
}*/ ll solve(int x,int y){
int fx = top[x],fy = top[y];
ll ans = ;
while(fx != fy){
if(dep[fx] < dep[fy]) swap(fx,fy),swap(x,y);
if(fx == ) ans += query(tid[fx]+,tid[x],,n+,);
else ans += query(tid[fx],tid[x],,n+,);
// cout<<x<<" "<<fx<<" "<<ans<<endl;
x = fa[fx]; fx = top[x];
}
if(x == y) return ans;
if(dep[x] > dep[y]) swap(x,y);
ans += query(tid[son[x]],tid[y],,n+,);
return ans;
} void init(){
cnt = cnt1 = ;
memset(son,-,sizeof(son));
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
} int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
init();
int tx,ty = n+;
for(int i = ;i <= n;i ++){
scanf("%d%d%lld",&a[i].u,&a[i].v,&a[i].val);
if(vis[a[i].u]&&vis[a[i].v]){
tx = a[i].u;
a[i].u = n+;
}
vis[a[i].u] = vis[a[i].v] = ;
add(a[i].u,a[i].v);
add(a[i].v,a[i].u);
}
// cout<<"jsjd: "<<tx<<" "<<ty<<endl;
dfs1(,,);
dfs2(,);
for(int i = ;i <= n;i ++){
if(dep[a[i].u] < dep[a[i].v])swap(a[i].u,a[i].v);
val[tid[a[i].u]] = a[i].val;
}
build(,n+,);
// ct(1,n+1,1);cout<<endl;
while(q--){
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op == ) update(tid[a[x].u],y,,n+,);
else{
ll num = solve(x,tx)+solve(y,ty);
ll num1 = solve(x,ty)+solve(y,tx);
//cout<<"num: "<<num<<"num1 : "<<num1<<"solve "<<solve(x,y)<<endl;
printf("%lld\n",min(solve(x,y),min(num,num1)));
}
}
}
return ;
}