zoukankan      html  css  js  c++  java
  • P4551 最长异或路径

    求树上两点使得两点路径xor和最大,可以用01字典树维护。
    假设1为根,d[i]表示1~i路径上xor和,每次查询x,y路径的xor和就是d[x] xor d[y],因为连续异或两个数字会抵消,相当于1~lca(x,y)的贡献消失了,那剩下的就是x到y的路径异或和
    注意01字典树取出x第i位写法

    #include<iostream>
    #include<cstdio>
    #define rep(i,j,k) for(register int i(j);i<=k;++i)
    #define drp(i,j,k) for(register int i(j);i>=k;--i)
    #define bug cout<<"~~~~~~~~~~~~~"<<'
    ';
    #define bugout(x) cout<<x<<endl;
    using std::cin;
    using std::cout;
    typedef long long lxl;
    template<typename T>
    inline T  max(T a, T b) {
    	return a > b ? a : b;
    }
    template<typename T>
    inline T  min(T a, T b) {
    	return a < b ? a : b;
    }
    
    inline char gt() {
    	static char buf[1 << 21], *p1 = buf, *p2 = buf;
    	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
    }
    template <typename T>
    inline void  read(T &x) {
    	register char ch = gt();
    	x = 0;
    	int w(0);
    	while(!(ch >= '0' && ch <= '9'))w |= ch == '-', ch = gt();
    	while(ch >= '0' && ch <= '9')x = x * 10 + (ch & 15), ch = gt();
    	w ? x = ~(x - 1) : x;
    }
    template <typename T>
    inline void out(T x, char cc) {
    	if(x < 0) x = -x, putchar('-');
    	char ch[20];
    	int num(0);
    	while(x || !num) ch[++num] = x % 10 + '0', x /= 10;
    	while(num) putchar(ch[num--]);
    	putchar(cc);
    }
    const int N = 2e5 + 79;
    
    struct graph {
    	int head[N], tot, next[N << 1], ver[N << 1], edge[N << 1];
    	inline void add(int a, int b, int c) {
    		ver[++tot] = b;
    		next[tot] = head[a];
    		head[a] = tot;
    		edge[tot] = c;
    	}
    } G;
    int d[N], n;
    struct Trie {
    	int ch[N * 27][2], tot;
        int val[N*27];
    	inline void insert(int x) {
    		int p =0;
    		drp(i,31,0){
    			int num((x>>i)&1);//right
    			if(!ch[p][num]) ch[p][num]=++tot;
    			p=ch[p][num];
    		}
             val[p]=x;
    	}
    	inline int query(int x){
    		int p=0;
    		drp(i,31,0){
    			int num((x>>i)&1 );
    			if(ch[p][num^1]) p=ch[p][num^1];
    			else p=ch[p][num];
    		}
    		return val[p];
    	}
    } T;
    
    
    inline void dfs(int x, int fa) {
    	for(register int i(G.head[x]); i; i = G.next[i]) {
    		int y(G.ver[i]), z(G.edge[i]);
    		if(y == fa) continue;
    		d[y] = d[x] ^ z;
    		dfs(y, x);
    	}
    }
    
    
    int main() {
    //	freopen("path.in", "r", stdin);
    //	freopen("path.out", "w", stdout);
    	read(n);
    	int x, y, z;
    	rep(i, 2, n) {
    		read(x);
    		read(y);
    		read(z);
    		G.add(x, y, z);
    		G.add(y, x, z);
    	}
    	dfs(1, 0);
    	int ans(0);
    	T.insert(0); //init
    	rep(i, 1, n) {
    		ans = max(ans, d[i] ^ T.query(d[i]));
    		T.insert(d[i]);
    	}
    	out(ans,'
    ');
    	return 0;
    }
    

    本文来自博客园,作者:{2519},转载请注明原文链接:https://www.cnblogs.com/QQ2519/p/15375313.html

  • 相关阅读:
    【Python之路】第三篇--Python基本数据类型
    【Python之路】第二篇--初识Python
    【Python之路】第一篇--Linux基础命令
    noip模拟测试7
    noip模拟测试6
    动态添加select的option [转载]
    javaweb报错:java.lang.NumberFormatException: null
    Javascript获取select的选中值和选中文本(转载)
    动态生成select框内容
    IO(Input&Output)流の介绍
  • 原文地址:https://www.cnblogs.com/QQ2519/p/15375313.html
Copyright © 2011-2022 走看看