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. 【WXS全局对象】Number

    属性: 名称 说明 Number.MAX_VALUE 返回JS中可表示的最大的数.它的近似值为 1.7976931348623157 x 10308. Number.MIN_VALUE 返回JS中可表 ...

  2. 剑指offer-二叉树中和为某一值的路径24

    题目描述 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大 ...

  3. Map Reduce Application(Top 10 IDs base on their value)

    Top 10 IDs base on their value First , we need to set the reduce to 1. For each map task, it is not ...

  4. truffle运行特殊 无法找到module的处理方法

    https://blog.csdn.net/SnWJy/article/details/80549227 错误描述: ​ truffle项目根目录执行truffle compile时,报错'modul ...

  5. 在mesh client示例中加入spi_slave接口(without IDE)

    在mesh client示例中加入spi_slave接口(without IDE) 主要是理解cmake构建的过程,然后修改工程中的inlcude路径及c源文件. 1. 解压mesh_sdk unzi ...

  6. sqlserver 2008 merger语句

    Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...

  7. BZOJ 4557 JLOI2016 侦查守卫 树形dp

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4557 题意概述: 给出一棵树,每个点付出代价w[i]可以控制距离和它不超过d的点,现在给 ...

  8. Bacon's Cipher(培根密码)

    Description Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a ...

  9. c# 委托初窥

    1.委托可以把方法当作参数在另一个方法中传递和调用 ,委托是方法的快捷方式. 2.委托是一个类. private void BeginSocketThread() { try { IPEndPoint ...

  10. Java之Math类使用小结(转发)

    Java的Math类封装了很多与数学有关的属性和方法,大致如下: public class Main { public static void main(String[] args) { // TOD ...