POJ - 2763 Housewife Wind

After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique. 


Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!' 


At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road. 


Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her? 
Input
The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001. 


The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000. 


The following q lines each is one of the following two types: 


Message A: 0 u 
A kid in hut u calls Wind. She should go to hut u from her current position. 
Message B: 1 i w 
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid. 
Output
For each message A, print an integer X, the time required to take the next child.
Sample Input
3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3
Sample Output
1

3

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
struct node{
	int to,nxt,vl;
}ed[maxn<<1];
int head[maxn],cnt;
void addedge(int u,int v,int vl){
	ed[cnt].to=v;
	ed[cnt].nxt=head[u];
	ed[cnt].vl=vl;
	head[u]=cnt++;
}
int sz[maxn],top[maxn],son[maxn],fa[maxn],dep[maxn],p[maxn];
int pos;
void dfs1(int u,int pre,int d){
	fa[u]=pre;
	sz[u]=1;
	dep[u]=d;
	for(int i=head[u];~i;i=ed[i].nxt){
		int v=ed[i].to;
		if(v==pre)continue;
		dfs1(v,u,d+1);
		if(son[u]==-1||sz[v]>sz[son[u]])son[u]=v;
		sz[u]+=sz[v];
	}
}
void dfs2(int u,int tp){
	top[u]=tp;
	p[u]=pos++;
	if(son[u]==-1)return;
	dfs2(son[u],tp);
	for(int i=head[u];~i;i=ed[i].nxt){
		int v=ed[i].to;
		if(v==son[u]||v==fa[u])continue;
		dfs2(v,v);
	}
}
int ban[maxn],fcc;
void change(int x,int p){
	while(x<=fcc){
		ban[x]+=p;
		x+=x&(-x);		
	}
}
int getsum(int x){
	int re=0;
	while(x){
		re+=ban[x];
		x-=x&(-x);
	}
	return re;
}

int getans(int u,int v){
	int f1=top[u],f2=top[v];
	int ans=0;
	while(f1!=f2){
		if(dep[f1]<dep[f2]){
			swap(u,v);
			swap(f1,f2);
		}
		ans+=getsum(p[u])-getsum(p[f1]-1);
		u=fa[f1];
		f1=top[u];
	}
	if(u==v)return ans;
	if(dep[u]<dep[v])swap(u,v);
	ans+=getsum(p[u])-getsum(p[v]);
	return ans;
} 

void init(){
	memset(head,-1,sizeof(head));
	memset(son,-1,sizeof(son));
	memset(ban,0,sizeof(ban));
	cnt=0;
	pos=1;
}
int main()
{
	int n,q,s;
	while(~scanf("%d%d%d",&n,&q,&s)){
		init();
		for(int i=1;i<n;i++){
			int u,v,vl;
			scanf("%d%d%d",&u,&v,&vl);
			addedge(u,v,vl);
			addedge(v,u,vl);
		}
		dfs1(1,0,0);
		dfs2(1,1);
		fcc=pos-1;
		for(int i=0;i<cnt;i+=2){
			int u=ed[i].to,v=ed[i^1].to,vl=ed[i].vl;
			if(dep[u]<dep[v])swap(u,v);
			change(p[u],vl);
		}
		while(q--){
			int a,b,w;
			scanf("%d%d",&a,&b);
			if(a==0){
				printf("%d\n",getans(s,b));
				s=b;
			}
			else {
				int u=ed[(b-1)*2].to,v=ed[(b-1)*2+1].to,vl=ed[(b-1)*2].vl;
				scanf("%d",&w);
				if(dep[u]<dep[v])swap(u,v);
				change(p[u],w-vl);
				ed[(b-1)*2].vl=w;
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值