其他分享
首页 > 其他分享> > POJ 2763 Housewife Wind LCA+树状数组 dfs序

POJ 2763 Housewife Wind LCA+树状数组 dfs序

作者:互联网

http://poj.org/problem?id=2763

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

题目大意:给一棵有n个节点数,给出n-1条边的关系及边的权值,给出你的初始节点,两种操作:(1)输出从当前节点到目标节点的路径长度;(2)修改第i条路径的权值。

思路:LCA+树状数组。不会树状数组的可以出门左拐了。在dfs的过程中我们可以得到dfs序,即第一次访问节点的时间戳(用数组in记录)和退出时的时间戳(用数组out记录),由此可以把这棵树当成一个区间来做。简单来说,第i个节点及其子树对应的区间就是[in[i],out[i]]。设给定的第i条边连接的两个节点是u和v,且deep[u]>deep[v],(节点u的深度要大于节点v)边权为dis,那么我们可做两个操作:add(in[u],dis),add(out[u]+1,-dis),此时从根节点1到pos节点的路径之和就是sum(pos)。(以上的add 和 sum操作是指树状数组的更新和求和操作) 不妨来分析一下正确性,当in[u]<=pos<=out[u]的时候,这条边的权值才对答案有贡献,显然这是正确的,因为根据dfs序的意义此时pos是节点u的子节点,要访问到pos节点自然要经过这条边。那么如何查询节点u和节点v的路径长度呢?其实跟不带修改操作的一样,答案就是sum(in[u])+sum(in[v])-2*sum(in[grand]),其中grand=LCA(u,v)。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;

const int maxn=1e5+5;

struct edge
{
    int to,nxt,dis;
};

struct e
{
    int u,v,dis;
}E[maxn];

edge Edge[maxn<<1];
int head[maxn];
int in[maxn];//dfs序
int out[maxn];
int fa[maxn];//父节点
int deep[maxn];//深度
int n,m,s,cnt=0,time=0;
int f[maxn][30];//LCA
int tree[maxn];//树状数组

inline int lowbit(int x)
{
    return x&(-x);
}

inline void update(int i,int v)
{
    for(;i<=n;i+=lowbit(i))
        tree[i]+=v;
}

inline int sum(int i)
{
    int ans=0;
    for(;i;i-=lowbit(i))
        ans+=tree[i];
    return ans;
}

inline void addedge(int u,int v,int dis)
{
    Edge[++cnt].to=v,Edge[cnt].nxt=head[u],Edge[cnt].dis=dis,head[u]=cnt;
    Edge[++cnt].to=u,Edge[cnt].nxt=head[v],Edge[cnt].dis=dis,head[v]=cnt;
}

void dfs(int u,int father)
{
    in[u]=++time;
    deep[u]=deep[father]+1;
    f[u][0]=father;
    if(u==1)
        for(int i=0;i<=20;i++)
            f[u][i]=u;
    else
        for(int i=1;i<=20;i++)
            f[u][i]=f[f[u][i-1]][i-1];
    for(int i=head[u];i;i=Edge[i].nxt)
    {
        if(Edge[i].to!=father)
            dfs(Edge[i].to,u);
    }
    out[u]=time;
}

inline int skip(int x,int level)
{
    for(int i=20;i>=0;i--)
        if((1<<i)&level)
            x=f[x][i];
    return x;
}

inline int LCA(int u,int v)
{
    if(deep[u]<deep[v])
        swap(u,v);
    u=skip(u,deep[u]-deep[v]);
    if(u==v)
        return u;
    for(int i=20;i>=0;i--)
        if(f[u][i]!=f[v][i])
            u=f[u][i],v=f[v][i];
    return f[u][0];
}

int main()
{
    int u,v,dis,grand;
    scanf("%d%d%d",&n,&m,&s);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].dis);
        addedge(E[i].u,E[i].v,E[i].dis);
    }
    dfs(1,0);
    for(int i=1;i<n;i++)
    {
        if(deep[E[i].v]>deep[E[i].u])
            update(in[E[i].v],E[i].dis),update(out[E[i].v]+1,-E[i].dis);
        else
            update(in[E[i].u],E[i].dis),update(out[E[i].u]+1,-E[i].dis);
    }
    int op;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&op,&v);
        if(op==0)
        {
            grand=LCA(s,v);
            printf("%d\n",sum(in[s])+sum(in[v])-2*sum(in[grand]));
            s=v;
        }
        else
        {
            scanf("%d",&dis);
            if(deep[E[v].u]>deep[E[v].v])
            {
                update(in[E[v].u],dis-E[v].dis);
                update(out[E[v].u]+1,E[v].dis-dis);
                E[v].dis=dis;
            }
            else
            {
                update(in[E[v].v],dis-E[v].dis);
                update(out[E[v].v]+1,E[v].dis-dis);
                E[v].dis=dis;
            }
        }
    }
    return 0;
}

 

标签:2763,update,Housewife,int,POJ,out,节点,Wind,dis
来源: https://blog.csdn.net/xiji333/article/details/98091636