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 修改边权的LCA 先把无根树dfs转化为有根树
然后根据深度进行 dis的更新
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + ;
int _pow[maxn], R[maxn], dis[maxn], vis[maxn], ver[maxn], dep[maxn];
int tot, head[maxn], dp[maxn * ][], k, first[maxn], val[maxn];
struct node {
int u, v, w, nxt;
} edge[maxn << ];
void init() {
tot = ;
mem(head, -);
}
void add(int u, int v, int w) {
edge[tot].v = v, edge[tot].u = u;
edge[tot].w = w, edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int d) {
vis[u] = ;
ver[++k] = u;
first[u] = k;
R[k] = d;
dep[u] = d;
for (int i = head[u]; ~i; i = edge[i].nxt) {
if (vis[edge[i].v]) continue;
int v = edge[i].v, w = edge[i].w;
dis[v] = dis[u] + w;
dfs(v, d + );
ver[++k] = u;
R[k] = d;
}
}
void ST(int len) {
int K = (int)(log((double)len) / log(2.0));
for (int i = ; i <= len ; i++) dp[i][] = i;
for (int j = ; j <= K ; j++) {
for (int i = ; i + _pow[j] - <= len ; i++) {
int a = dp[i][j - ], b = dp[i + _pow[j - ]][j - ];
if (R[a] < R[b]) dp[i][j] = a;
else dp[i][j] = b;
}
}
}
int RMQ(int x, int y) {
int K = (int)(log((double)(y - x + )) / log(2.0));
int a = dp[x][K], b = dp[y - _pow[K] + ][K];
if (R[a] < R[b]) return a;
else return b;
}
int LCA(int u, int v) {
int x = first[u], y = first[v];
if (x > y) swap(x, y);
int ret = RMQ(x, y);
return ver[ret];
}
void update(int u, int fa, int ret) {
dis[u] += ret;
for (int i = head[u]; ~i ; i = edge[i].nxt) {
int v = edge[i].v;
if (v == fa) continue;
update(v, u, ret);
}
}
int main() {
for (int i = ; i < ; i++) _pow[i] = ( << i);
int n, q, s;;
while(~sfff(n, q, s)) {
init();
mem(vis, );
mem(dep, );
for (int i = ; i < n - ; i++) {
int u, v, w;
sfff(u, v, w);
add(u, v, w);
add(v, u, w);
}
k = , dis[] = ;
dfs(, );
ST( * n - );
while(q--) {
int op, i, w, t;
sf(op);
if (op) {
sff(i, w);
i = (i - ) << ;
int u = edge[i].u, v = edge[i].v;
int ret = w - edge[i].w;
edge[i].w = edge[i ^ ].w = w;
int x = dep[u] > dep[v] ? u : v;
int y = dep[u] < dep[v] ? u : v;
update(x, y, ret);
} else {
sf(t);
int lca = LCA(s, t);
printf("%d\n", dis[s] + dis[t] - * dis[lca]);
s = t;
}
}
}
return ;
}

POJ 2763 Housewife Wind 纯粹LCA写法(简单无脑)的更多相关文章

  1. POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

    题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...

  2. POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )

    POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...

  3. poj 2763 Housewife Wind(树链拆分)

    id=2763" target="_blank" style="">题目链接:poj 2763 Housewife Wind 题目大意:给定一棵 ...

  4. POJ 2763 Housewife Wind(DFS序+LCA+树状数组)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 11419   Accepted: 3140 D ...

  5. POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 10378   Accepted: 2886 D ...

  6. LCA+树状数组 POJ 2763 Housewife Wind

    题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...

  7. POJ - 2763 Housewife Wind (树链剖分/ LCA+RMQ+树状数组)

    题意:有一棵树,每条边给定初始权值.一个人从s点出发.支持两种操作:修改一条边的权值:求从当前位置到点u的最短路径. 分析:就是在边可以修改的情况下求树上最短路.如果不带修改的话,用RMQ预处理LCA ...

  8. POJ 2763"Housewife Wind"(DFS序+树状数组+LCA)

    传送门 •题意 一对夫妇居住在 xx村庄,给村庄有 $n$ 个小屋: 这 $n$ 个小屋之间有双向可达的道路,不会出现环,即所构成的图是个树: 从 $a_i$ 小屋到 $b_i$ 小屋需要花费 $w_ ...

  9. poj 2763 Housewife Wind(树链剖分+单点查询+区间修改)

    题目链接:http://poj.org/problem?id=2763 题意:给一个数,边之间有权值,然后两种操作,第一种:求任意两点的权值和,第二,修改树上两点的权值. 题解:简单的树链剖分. #i ...

随机推荐

  1. informix如何查询第一条记录

    1.select first 1 * from shop; 正序查询第一条数据 2.select first 1 * from shop order by create_time desc; 按创建时 ...

  2. HDU - 3415(DP + 单调队列)

    链接:HDU - 3415 题意:给出一个包含 n 个数的环,求满足长度大于 0 小于等于 k 的最大区间和. 题解:将数组加倍,形成环.求一个前缀和sum.枚举每一个sum[i],以 i 结尾的最大 ...

  3. 四分树 (Quadtrees UVA - 297)

    题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...

  4. [Clr via C#读书笔记]Cp17委托

    Cp17委托 简单介绍 delegate回调函数机制,可以理解存储函数地址的变量类型: 类型安全: 引用类型支持逆变和协变: 回调 静态方法,实例方法 委托的本质 所有的委托都派生自System.Mu ...

  5. Ubuntu14.04下部署FastDFS 5.08+Nginx 1.9.14

      最新的版本可以在这里获取,目前下载的最新版本是5.08,更新于2016-02-03.在这里可以找到更多的说明. 下载好后,server端分为两个部分,一个是tracker,一个是storage.顾 ...

  6. str和repr

    在Python2.6和Python3.0以及更早的版本中,在交互式模式下的输出本质上是使用repr,因此对于一些浮点数运算,会显示很多位: 4 / 5.0 #0.8000000000000004 但是 ...

  7. Bad Cowtractors(最大生成树)

      Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= ...

  8. 软工2017团队协作第七周——个人PSP

    10.27 --11.2本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 开始时间                结束时间 中断时间 实际用时 ...

  9. nginx的平滑升级,不间断服务

    nginx的平滑升级,不间断服务   Nginx更新真的很快,最近nginx的1.0.5稳定版,nginx的0.8.55和nginx的0.7.69旧的稳定版本已经发布.我一项比较喜欢使用新版本的软件, ...

  10. php添加扩展 在phpinfo能看到该扩展,但在cli用php -m 却看不到,为什么呢,求指教

    1. 没有出现的原因是:执行时添加上php.ini的文件就可以了    $ /usr/local/php/bin/php -c /usr/local/php/etc/php.ini -m | grep ...