zoukankan      html  css  js  c++  java
  • HDU

    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.
    2≤n,q≤105
    0≤Vi≤10^9
    1≤Fi≤n, the root of the tree is node 1.
    1≤u≤n,0≤x≤10^9
    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

    题解:

    01字典树就不多说了,不会的这题也没必要做了。DFS序就是对一个树跑一边DFS,遍历点的顺序就是DFS序。

    主要是启发式合并,这题做法其实就是给每个点建立一个01字典树,而通过DFS可以保证每个点的01字典树中的点都是其自身或子节点,而启发式合并的作用就在于节省了建立01字典树的空间和时间。

    ps:启发式合并时间复杂度o(nlogn*插入复杂度的)。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    typedef struct Node* node;
    
    const int MAXN = 1e5+10;
    
    struct Edge{
    	int to,next;
    }E[MAXN*4];
    
    int head[MAXN],tot;
    int board[MAXN];//存点的值 
    int re[MAXN];//存每个问题的结果 
    vector< pair<int,int> >P[MAXN];//分别存以每个点为根节点的问题。 
    
    void Add(int from,int to){
    	E[++tot].next = head[from];
    	head[from] = tot;
    	E[tot].to = to;
    }
    
    struct Node
    {
    	int num,val;
    	node Next[2];
    	Node()
    	{
    		val = num = 0;
    		memset(Next,NULL,sizeof(Next));
    	}
    }*root[MAXN];
    
    void Insert(node root,int x,int flag)
    {
    	node p = root;
    	for(int i=31 ; i>=0 ; i--)
    	{
    		int t = (x>>i)&1;
    		if(p->Next[t] == NULL)p->Next[t] = new struct Node();
    		p->num += flag;
    		p = p->Next[t]; 
    	}
    	p->num += flag;
    	p->val = x;
    }
    
    int Judge(node root,int x)
    {
    	node p = root;
    	for(int i=31 ; i>=0 ; i--)
    	{
    		int t = ((x>>i)&1)^1;
    		if(p->Next[t] == NULL || p->Next[t]->num == 0)t = (x>>i)&1;
    		if(p->Next[t] && p->Next[t]->num)p = p->Next[t];
    		else return 0;
    	}
    	return p->val;
    } 
    
    void Del(node root){
    	for(int i=0 ; i<2 ; ++i){
    		if(root->Next[i])Del(root->Next[i]);
    	}
    	delete(root);
    }
    
    inline void init(){
    	memset(head,0,sizeof head);
    	tot = 0;
    }
    
    node Merge(node p,node q){
    	if(p == NULL)return q;
            else if(q == NULL)return p;
            else p->num += q->num;
    	p->Next[0] = Merge(p->Next[0],q->Next[0]);
    	p->Next[1] = Merge(p->Next[1],q->Next[1]);
    	free(q);
    	return p;
    }
    
    void DFS(int now,int father){
    	root[now] = new struct Node();
    	Insert(root[now],board[now],1);
    	for(int i=head[now] ; i ; i=E[i].next){
    		if(E[i].to == father)continue; 
    		DFS(E[i].to,now);
    		root[now] = Merge(root[now],root[E[i].to]);
    	}
    	for(int i=0 ; i<P[now].size() ; ++i){
    		re[P[now][i].first] = P[now][i].second^Judge(root[now],P[now][i].second);
    	}
    }
    
    int main(){
    	
    	int N,Q;
    	while(scanf("%d %d",&N,&Q)!=EOF){
    		init();
    		for(int i=1 ; i<=N ; ++i){
    			scanf("%d",&board[i]);
    			P[i].clear();
    		}
    		for(int i=2 ; i<=N ; ++i){
    			int t;
    			scanf("%d",&t);
    			Add(t,i);
    		}
    		for(int i=1 ; i<=Q ; ++i){
    			int a,b;
    			scanf("%d %d",&a,&b);
    			P[a].push_back(make_pair(i,b));//pair.first代表问题进来的顺序 
    		} 
    		DFS(1,-1);
    		for(int i=1 ; i<=Q ; ++i)printf("%d
    ",re[i]);
    		Del(root[1]);
    	}
    	
    	return 0;
    }


  • 相关阅读:
    【spring-boot】spring-boot 事物管理——之注解@Transactional
    【spring-boot】Spring-boot学习-helloWorld
    【Maven】failOnMissingWebXml
    【java基础领域】高内聚、低耦合理解
    Pascal's Triangle
    Remove Nth Node From End of List
    Valid Palindrome
    Longest Common Prefix
    Roman to Integer
    Palindrome Number
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514065.html
Copyright © 2011-2022 走看看