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. TW实习日记:第20-21天

    为什么上周五没写呢,因为上周五一直在熟悉业务流程...根本不会写一些复杂的业务代码,因为没有业务流程图!!!在学校的上需求分析和UML建模课的时候,还有软件工程课的时候,想着这都什么鬼啊,听来干嘛,写 ...

  2. yii的学习笔记 基本结构 自用

    Yii 学习笔记 W:YII是什么? Q:Yii 是一个基于组件的高性能 PHP 框架,用于快速开发大型 Web 应用.它使Web开发中的 可复用度最大化,可以显著提高你的Web应用开发速度.Yii ...

  3. Python3 Tkinter-Message

    1.创建 from tkinter import * root=Tk() Message(root,text='hello Message').pack() root.mainloop() 2.属性 ...

  4. 【转】Hbuilder MUI 页面刷新及页面传值问题

    文章来源:http://www.111cn.net/sys/CentOS/67213.htm 一.页面刷新问题 1.父页面A跳转到子页面B,B页面修改数据后再跳回A页面,刷新A页面数据 (1).父页面 ...

  5. windows编程常见数据类型

    windows编程常见数据类型, 总结一下方便查阅: 类型 对应指针 描述 ATOM . typedef WORD ATOM; BOOL LPBOOL 布尔类型,值要写成TRUE或FALSE,实际上是 ...

  6. RabbitMQ基本模式

    最近用到了一些RabbitMQ的东西,看了官方的Get Started,以此为模板总结一下. (1)生产者(发送方)发送消息到ExChange(含参:routingkey),ExChange通过bin ...

  7. 买卖股票的最佳时机I II III IV

    I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...

  8. lintcode-161-旋转图像

    161-旋转图像 给定一个N×N的二维矩阵表示图像,90度顺时针旋转图像. 样例 给出一个矩形[[1,2],[3,4]],90度顺时针旋转后,返回[[3,1],[4,2]] 挑战 能否在原地完成? 标 ...

  9. Jenkins系列-Jenkins忘记密码的修复方法

    找回 admin 用户的密码后,可以登录系统修改其他用户的密码. 1. Jenkins 目录结构 Jenkins 没有使用数据库,所有的信息都保存在 JENKINS_HOME 目录下的文件中.其中 J ...

  10. docker配置网络

    1.暂停服务,删除旧网桥#service docker stop#ip link set dev docker0 down#brctl delbr docker0 2.创建新网桥bridge0#brc ...