poj2763(树链剖分)
来源:互联网 发布:华为手机网络诊断 编辑:程序博客网 时间:2024/06/05 07:19
Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 11560 Accepted: 3173
Description
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 <iostream>#include <stdio.h>#include <string.h>#include <vector>using namespace std;const int N=100005;int pre[N],son[N];int top[N],dep[N];int siz[N];int ran[N],tid[N];vector< vector<int> > vec(N);int id;void init(int n){ for(int i=1;i<=n;i++){ vec[i].clear(); } memset(pre,-1,sizeof(pre)); memset(son,-1,sizeof(son)); memset(top,0,sizeof(top)); memset(dep,0,sizeof(dep)); memset(siz,0,sizeof(siz)); memset(ran,0,sizeof(ran)); memset(tid,0,sizeof(tid)); id=0;}void dfs_1(int u,int p,int d){ siz[u]=1; pre[u]=p; dep[u]=d; for(int i=0;i<vec[u].size();i++){ int v=vec[u][i]; if(v!=p){ dfs_1(v,u,d+1); siz[u]+=siz[v]; if(son[u]==-1||siz[v]>siz[son[u]]){ son[u]=v; } } }}void dfs_2(int u,int tp){ top[u]=tp; tid[u]=(++id); ran[id]=u; if(son[u]==-1){ return; } dfs_2(son[u],tp); for(int i=0;i<vec[u].size();i++){ int v=vec[u][i]; if(v!=son[u]&&v!=pre[u]){ dfs_2(v,v); } }}struct Edge{ int l,r; int num;}a[4*N];void pushup(int n){ a[n].num=a[2*n].num+a[2*n+1].num;}void build(int l,int r,int node){ a[node].l=l; a[node].r=r; a[node].num=0; if(l==r){ return; } int mid=(l+r)/2; build(l,mid,2*node); build(mid+1,r,2*node+1); pushup(node);}void update(int index,int node,int value){ if(a[node].l==index && a[node].r==index){ a[node].num=value; return; } int mid=(a[node].l+a[node].r)/2; if(index<=mid){ update(index,2*node,value); } else { update(index,2*node+1,value); } pushup(node);}int query(int l,int r,int node){ if(a[node].l==l&&a[node].r==r){ return a[node].num; } int mid=(a[node].l+a[node].r)/2; if(r<=mid){ return query(l,r,2*node); } else if(l>mid){ return query(l,r,2*node+1); } else{ return query(l,mid,2*node)+query(mid+1,r,2*node+1); }}void solve(int u,int v){ int sum=0; int fu=top[u]; int fv=top[v]; while(fu!=fv){ if(dep[fu]<dep[fv]){ swap(fu,fv); swap(u,v); } sum+=query(tid[fu],tid[u],1); u=pre[fu]; fu=top[u]; } if(u==v){ printf("%d\n",sum); return; } if(dep[u]>dep[v]){ swap(u,v); } sum+=query(tid[son[u]],tid[v],1); printf("%d\n",sum); return;}int e[N][4];int main(){ int n,q,s,u,v,w; while(~scanf("%d %d %d",&n,&q,&s)){ init(n); for(int i=0;i<n-1;i++){ scanf("%d %d %d",&e[i][0],&e[i][1],&e[i][2]); vec[e[i][0]].push_back(e[i][1]); vec[e[i][1]].push_back(e[i][0]); } dfs_1(1,-1,0); dfs_2(1,1); build(1,id,1); for(int i=0;i<n-1;i++){ if(dep[e[i][0]]>dep[e[i][1]]){ swap(e[i][0],e[i][1]); } update(tid[e[i][1]],1,e[i][2]); } while(q--){ int k; scanf("%d",&k); if(k==0){ scanf("%d",&u); solve(s,u); s=u; } else { scanf("%d %d",&u,&w); update(tid[e[u-1][1]],1,w); } } } return 0;}
- poj2763(树链剖分)
- POJ2763 树链剖分
- POJ2763 树链剖分
- 树链剖分(基于边权)poj2763
- poj2763 Housewife Wind 树链剖分
- 树链剖分 - poj2763 Housewife Wind
- 【POJ2763】Housewife Wind-树链剖分
- POJ2763
- poj2763--Housewife Wind(树链剖分+线段树)
- 树链剖分+线段树 poj2763 Housewife Wind
- poj2763 Housewife Wind【树链剖分+线段树】
- 【POJ2763】Housewife Wind(树链剖分+线段树(基于边权))
- poj2763 LCT
- 【动态树】poj2763
- Housewife Wind poj2763
- poj2763 Housewife Wind
- poj2763 Housewife Wind
- POJ2763 Housewife Wind
- Android学习笔记之图像颜色处理(ColorMatrix)
- 鼠标测试工具Simple_Mouse_Test
- ACM 迭代深搜法 Power Calculus
- 用卷积公式求概率密度时确定积分区间
- 非线性结构——树
- poj2763(树链剖分)
- 远程桌面(服务器)的链接步骤
- 大数除法之估商法源码
- 百练OJ:3865和2677:肿瘤检测
- 移动UI设计:基本类型屏幕
- 选择排序算法(node)
- Android okhttp3来进行网络数据请求和发送
- 多进程multiprocessing-进程池Pool
- java 猜拳小游戏