码迷,mamicode.com
首页 > 其他好文 > 详细

Hdu-4757 Tree(可持久化字典树+lca)

时间:2018-05-01 15:28:59      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:can   efi   ash   tun   series   ora   auth   idea   res   

题目链接:点这
我的github地址:点这
 
 
Problem Description
  Zero and One are good friends who always have fun with each other. This time, they decide to do something on a tree which is a kind of graph that there is only one path from node to node. First, Zero will give One an tree and every node in this tree has a value. Then, Zero will ask One a series of queries. Each query contains three parameters: x, y, z which mean that he want to know the maximum value produced by z xor each value on the path from node x to node y (include node x, node y). Unfortunately, One has no idea in this question. So he need you to solve it.
 

 

Input
  There are several test cases and the cases end with EOF. For each case:

  The first line contains two integers n(1<=n<=10^5) and m(1<=m<=10^5), which are the amount of tree’s nodes and queries, respectively.

  The second line contains n integers a[1..n] and a[i](0<=a[i]<2^{16}) is the value on the ith node.

  The next n–1 lines contains two integers u v, which means there is an connection between u and v.

  The next m lines contains three integers x y z, which are the parameters of Zero’s query.
 

 

Output
  For each query, output the answer.
 

 

Sample Input

3 2 1 2 2 1 2 2 3 1 3 1 2 3 2
 

 

Sample Output

3 0
 

 

Source

 

题意:给一棵带权树,每次询问树上一条链上(x 到 y)的所有权值xor z的最大值。

思路:可持久化字典树+lca

不能用lca的倍增算法,会t,

关于lca可以看这个:https://www.cnblogs.com/zhouzhendong/p/7256007.html

/*
    data:2018.04.26
    author:gswycf
    link:http://acm.hdu.edu.cn/showproblem.php?pid=4825
    accout:tonysave
*/
#define ll long long
#define IO ios::sync_with_stdio(false);
#define maxn 100005

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<vector>
using namespace std;

class Node{
    public:
        int cnt,ls,rs;
};
int n,m,cnt,aa,bb;
Node tr[32*maxn];
int weight[maxn];int root[maxn];int deep[maxn],f[maxn][31];
vector<int>  g[maxn];

inline void init()
{
    memset(tr,0,sizeof(tr));
    //memset(weight,0,sizeof(weight));
    memset(root,0,sizeof(root));
    deep[1]=0;
    cnt=0;
    for(int i=0;i<=n;i++)
        g[i].clear();
}
int in(int pre,int x,int deep)
{
    int num=++cnt;
    tr[num]=tr[pre];
    tr[num].cnt=tr[pre].cnt+1;
    if(deep<0)return num;
    if(!((x>>deep)&1))tr[num].ls=in(tr[pre].ls,x,deep-1);
    else tr[num].rs=in(tr[pre].rs,x,deep-1);
    return num;
}
int query(int l,int r,int x,int deep)
{
    if(deep<0)return 0;
    if(!((x>>deep)&1))
    {
        if(tr[tr[r].rs].cnt>tr[tr[l].rs].cnt)return (1<<deep)+query(tr[l].rs,tr[r].rs,x,deep-1);
        else return query(tr[l].ls,tr[r].ls,x,deep-1);
    }
    else
    {
        if(tr[tr[r].ls].cnt>tr[tr[l].ls].cnt)return (1<<deep)+query(tr[l].ls,tr[r].ls,x,deep-1);
        else return query(tr[l].rs,tr[r].rs,x,deep-1);
    }
}
void bfs(int node,int fa)
{
    root[node]=in(fa,weight[node],16);
    f[node][0]=fa;
    for(int i=0;i<g[node].size();i++)
    {
        if(g[node][i]!=fa)
        {
            deep[g[node][i]]=deep[node]+1;
            bfs(g[node][i],node);
        }
    }
}
inline void init2()
{
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i<=n;i++)
            f[i][j]=f[f[i][j-1]][j-1];
}
int lca(int a,int b,int c)
{
    aa=a,bb=b;
    if(deep[a]>deep[b])swap(a,b);
    int d=deep[b]-deep[a];
    for(int i=0;i<30;i++)
        if((1<<i)&d)b=f[b][i];
    if(a==b)return a;
    for(int i=29;i>=0;i--)
    {
        if(f[a][i]!=f[b][i])
            a=f[a][i],b=f[b][i];
    }
    b=f[b][0];
    return b;
    
   
}

int main()
{
    int a,b,c;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1;i<=n;i++)scanf("%d",&weight[i]);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g[b].push_back(a);
        }
        bfs(1,0);init2();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            int k=lca(a,b,c);
            printf("%d\n",max(query(root[k-1],root[a],c,16),query(root[k-1],root[b],c,16)));
        }
    }
}

 

Hdu-4757 Tree(可持久化字典树+lca)

标签:can   efi   ash   tun   series   ora   auth   idea   res   

原文地址:https://www.cnblogs.com/fantastic123/p/8976091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!