HDU6191-Query on A Tree

Query on A Tree

                                                                   Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
                                                                                                 Total Submission(s): 475    Accepted Submission(s): 182


Problem Description
Monkey A lives on a tree, he always plays on this tree.

One day, monkey A learned about one of the bit-operations, xor. He was keen of this interesting operation and wanted to practise it at once.

Monkey A gave a value to each node on the tree. And he was curious about a problem.

The problem is how large the xor result of number x and one node value of label y can be, when giving you a non-negative integer x and a node label u indicates that node y is in the subtree whose root is u(y can be equal to u).

Can you help him?
 

Input
There are no more than 6 test cases.

For each test case there are two positive integers n and q, indicate that the tree has n nodes and you need to answer q queries.

Then two lines follow.

The first line contains n non-negative integers  V1,V2,,Vn , indicating the value of node i.

The second line contains n-1 non-negative integers  F1,F2,Fn1 Fi  means the father of node  i+1 .

And then q lines follow.

In the i-th line, there are two integers u and x, indicating that the node you pick should be in the subtree of u, and x has been described in the problem.

2n,q105

0Vi109

1Fin , the root of the tree is node 1.

1un,0x109
 

Output
For each query, just print an integer in a line indicating the largest result.
 

Sample Input
  
  
2 2 1 2 1 1 3 2 1
 

Sample Output
  
  
2 3
 

Source
 


题意:给你一棵以1为根的树,每个节点有一个值,有q次询问,问x和以u为根节点子树中的一个节点的值异或后最大为多少

解题思路:字典树+启发式合并(注意启发式合并不能在线询问)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m, a[100009], b[35], fa[100009];
int ss[100009], f[35 * 100009][2], tot;
int cnt, s[100009], nt[100009], e[100009];
vector<pair<int, int> >g[100009];
int ans[100009];

void merge(int &now, int pre)
{
    if (!now || !pre) { now = now^pre; return; }
    merge(f[now][0], f[pre][0]);
    merge(f[now][1], f[pre][1]);
}

void dfs(int k)
{
    for (int i = s[k]; ~i; i = nt[i])
    {
        dfs(e[i]);
        merge(ss[k], ss[e[i]]);
    }
    int Size = g[k].size();
    for (int i = 0; i < Size; i++)
    {
        int x = g[k][i].first;
        for (int i = 1; i <= 32; i++) b[i] = x % 2, x /= 2;
        int p = ss[k], ans1 = 0;
        for (int i = 32; i >= 1; i--)
        {
            ans1 <<= 1;
            if (f[p][b[i] ^ 1]) ans1 |= 1, p = f[p][b[i] ^ 1];
            else p = f[p][b[i]];
        }
        ans[g[k][i].second] = ans1;
    }
}

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        tot = cnt = 0;
        memset(s, -1, sizeof s);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]), g[i].clear();
        for (int i = 2; i <= n; i++)
        {
            scanf("%d", &fa[i]);
            nt[cnt] = s[fa[i]], s[fa[i]] = cnt, e[cnt++] = i;
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= 32; j++) b[j] = a[i] % 2, a[i] /= 2;
            ss[i] = ++tot;
            memset(f[tot], 0, sizeof f[tot]);
            for (int j = 32, p = tot; j >= 1; j--)
            {
                int k = b[j];
                if (!f[p][k])
                {
                    f[p][k] = ++tot;
                    memset(f[tot], 0, sizeof f[tot]);
                }
                p = f[p][k];
            }
        }
        for (int i = 1; i <= m; i++)
        {
            int u, x;
            scanf("%d%d", &u, &x);
            g[u].push_back(make_pair(x, i));
        }
        dfs(1);
        for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值