HDOJ 题目2763 Housewife Wind(Link Cut Tree修改边权,查询两点间距离)

Housewife Wind

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 7639   Accepted: 1990

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

Source

POJ Monthly--2006.02.26,zgl & twb

题目大意:操作 0 查询当前点到这个点的最短距离,操作1修改第k个边的权值

Problem: 2763		User: kxh1995
Memory: 7636K		Time: 1141MS
Language: C++		Result: Accepted

ac代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<iostream>
using namespace std;
int head[100010],cnt,vis[100050];
struct s
{
    int u,v,w,next;
}edge[150050<<1];
struct LCT
{
    int bef[150050],pre[150050],next[150050][2],key[150050],sum[150050],belong[150050];
    void init()
    {
        memset(pre,0,sizeof(pre));
        memset(next,0,sizeof(next));
    }
    void pushup(int x)
    {
        sum[x]=key[x]+sum[next[x][1]]+sum[next[x][0]];
    }
    void rotate(int x,int kind)
    {
        int y,z;
        y=pre[x];
        z=pre[y];
        next[y][!kind]=next[x][kind];
        pre[next[x][kind]]=y;
        next[z][next[z][1]==y]=x;
        pre[x]=z;
        next[x][kind]=y;
        pre[y]=x;
        pushup(y);
    }
    void splay(int x)
    {
        int rt;
        for(rt=x;pre[rt];rt=pre[rt]);
        if(x!=rt)
        {
            bef[x]=bef[rt];
            bef[rt]=0;
            while(pre[x])
            {
                if(next[pre[x]][0]==x)
                {
                    rotate(x,1);
                }
                else
                    rotate(x,0);
            }
            pushup(x);
        }
    }
    void access(int x)
    {
        int fa;
        for(fa=0;x;x=bef[x])
        {
            splay(x);
            pre[next[x][1]]=0;
            bef[next[x][1]]=x;
            next[x][1]=fa;
            pre[fa]=x;
            bef[fa]=0;
            fa=x;
            pushup(x);
        }
    }
    void change(int x,int val)
    {
        int t;
        t=belong[x-1];
        key[t]=val;
        splay(t);
    }
    int query(int x,int y)
    {
        access(y);
        for(y=0;x;x=bef[x])
        {
            splay(x);
            if(!bef[x])
                return sum[y]+sum[next[x][1]];
            pre[next[x][1]]=0;
            bef[next[x][1]]=x;
            next[x][1]=y;
            pre[y]=x;
            bef[y]=0;
            y=x;
            pushup(x);
        }
    }
}lct;
void add(int u,int v,int w)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void bfs(int u)
{
    int i,y;
    queue<int>q;
    memset(vis,0,sizeof(vis));
    vis[u]=1;
    q.push(u);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(!vis[v])
            {
                lct.bef[v]=u;
                lct.key[v]=lct.sum[v]=edge[i].w;
                lct.belong[i>>1]=v;
                vis[v]=1;
                q.push(v);
            }
        }
    }
}
int main()
{
	int n,m,s;
	while(scanf("%d%d%d",&n,&m,&s)!=EOF)
	{
		int i;
		lct.init();
		cnt=0;
		memset(head,-1,sizeof(head));
		for(i=1;i<n;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w);
			add(v,u,w);
		}
		bfs(1);
		while(m--)
		{
			int op;
			scanf("%d",&op);
			if(!op)
			{
				int x;
				scanf("%d",&x);
				printf("%d\n",lct.query(s,x));
				s=x;
			}
			else
			{
				int x,y;
				scanf("%d%d",&x,&y);
				lct.change(x,y);
			}
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-15 18:42:21

HDOJ 题目2763 Housewife Wind(Link Cut Tree修改边权,查询两点间距离)的相关文章

HDOJ 题目2475 Box(link cut tree去点找祖先)

Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2374    Accepted Submission(s): 718 Problem Description There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes

URAL 题目1553. Caves and Tunnels(Link Cut Tree 修改点权,求两点之间最大)

1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that ther

URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)

1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that ther

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

POJ 2763 Housewife Wind (树链剖分+线段树)

题目链接:POJ 2763 Housewife Wind 题意:抽象出来就是 一棵已知节点之间的边权,两个操作,1·修改边权,2·询问两个节点之间的边权和. AC代码: #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int

脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现还是可以写的,只需要实时交换答案二元组里的两棵树,最后在吧提出来的访问节点放回去就行了.本着只学一种平衡树的想法,脑洞大开加偏执人格的开始写可持久化Treap版的Link Cut Tree... 写了才发现,常数硕大啊!!!代码超长啊!!!因为merge是从上到下,split从下到上,pushdow

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

题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为d[u]+d[v]-2*d[LCA(u,v)] 现在d数组是变化的 对应每一条边的变化 他修改的是一个区间 用时间戳处理每个点管辖的区域 然后用线段树修改 线段树的叶子节点村的是根到每一个点的距离 求最近公共祖先没差别 只是堕落用线段树维护d数组 各种错误 4个小时 伤不起 #include <cs

LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的. 1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接. 2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在. 3:后接两个整数(x,y),代表将点x上的权值变成y. 输入输出

Link Cut Tree学习笔记

从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子树信息 小结 动态树问题和Link Cut Tree 动态树问题是一类要求维护一个有根树森林,支持对树的分割, 合并等操作的问题. Link Cut Tree(林可砍树?简称LCT)是解决这一类问题的一种数据结构. 一些无聊的定义 Link Cut Tree维护的是动态森林中每棵树的任意链剖分. P