1 条题解

  • 1
    @ 2021-8-7 20:49:57

    C++ :

    #include <bits/stdc++.h>
    #define LL long long 
    using namespace std;
    const int maxn=2*1e5+100;
    LL v[maxn],ver[maxn],edge[maxn],nextr[maxn],head[maxn],sum[maxn],trie[2][maxn*16];
    int n,tot=1;
    
    inline LL read()//快速读入
    {
        char ch=getchar();
        int p=1,q=0;
        for(; ch!='-' && ch<'0' || ch>'9'; ch=getchar());
        if (ch=='-')
            p=-p;
        for(; ch>='0' && ch<='9'; q=q*10+ch-'0',ch=getchar());
        return p*q;
    }
    
    inline void add(int x,int y,int z){
    	ver[++tot] = y,edge[tot] = z;
    	nextr[tot] = head[x],head[x] = tot;
    }
    
    inline void dfs(int x){
    	v[x] = 1;
    	for (int i=head[x];i;i=nextr[i]){
    		if (!v[ver[i]]){
    			sum[ver[i]] = sum[x] ^ edge[i];
    			dfs(ver[i]);
    		}
    	}
    }
    
    inline void insert(int x){
    	int p = 1;
    	for (int k=31;k>=0;k--){
    		int ch = x >>k &1;
    		if (trie[ch][p] == 0) trie[ch][p] =++tot;
    		p = trie[ch][p];
    	}
    }
    
    inline LL search(int x){
    	LL p = 1,ans = 0;
    	for (int k=31;k>=0;k--){
    		int ch = x>>k&1;
    		if (trie[ch^1][p]) {
    			p = trie[ch^1][p];
    			ans|=(1<<k);
    		}else{
    			p = trie[ch][p];
    		}
    	}
    	return ans;
    }
    
    int main(){
    	cin >> n;
    	memset(v,0,sizeof(v));
    	for (int i=1;i<n;i++){
    		LL x,y,z;
    		x = read(),y = read(),z = read();
    		x++,y++;
    		add(x,y,z);
    		add(y,x,z);
    	}
    	memset(sum,0,sizeof(sum));
    	dfs(1);
    	tot =1;
    	LL ans  = 0;
    	for (int i=1;i<=n;i++){
    		insert(sum[i]);
    		ans = max(ans,search(sum[i]));
    	}
    	cout << ans ;
    
    }
    
    • 1

    信息

    ID
    55
    时间
    1000ms
    内存
    128MiB
    难度
    4
    标签
    递交数
    100
    已通过
    50
    上传者