why RE
查看原帖
why RE
999274
CNS_5t0_0r2楼主2023/12/4 13:53
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9,LOGN = 31,K = N << 1;
struct edge{
	int to,cost,nex;
} e[K];
int head[K],ecnt;
int xor_num[K];
int n,ans;
void addedge(int u,int v,int w){
	ecnt++;
	e[ecnt] = (edge){v,w,head[u]};
	head[u] = ecnt;
}
void dfs(int u,int fa,int num){
	xor_num[u] = num;
	for(int i = head[u];i;i = e[i].nex){
		int v = e[i].to,w = e[i].cost;
		if(v == fa)
			continue;
		dfs(v,u,num ^ w);
	}
}
struct node{
    int child[2];//所有子节点,编号为i的子节点表示字符对应的ASCII码编号 
} trie[K];
int node_cnt;//节点总数
void insert(int x){//插入
    int u = 0;//u表示当前节点的指针,从空节点0开始 
    for(int i = LOGN;i >= 0;i--){
        int c = (x >> i) & 1;
        if(!trie[u].child[c])//如果节点不存在
            trie[u].child[c] = ++node_cnt;//新建节点
        u = trie[u].child[c];//指向子节点
    }
}
int query(int x){
    int u = 0,ans = 0;
    for(int i = LOGN;i >= 0;i--){
        int c = (x >> i) & 1;
        if(trie[u].child[!c]){
            u = trie[u].child[!c];
            ans = (ans << 1) | 1;
        }
        else{
            u = trie[u].child[c];
            ans <<= 1;
        }
    }
    return ans;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n - 1;i++){
		int u,v,w;
		cin >> u >> v >> w;
		addedge(u,v,w);
		addedge(v,u,w);
	}
	dfs(1,0,0);
	for(int i = 1;i <= n;i++)
		insert(xor_num[i]);
	for(int i = 1;i <= n;i++)
        ans = max(ans,query(xor_num[i]));
    cout << ans;
	return 0;
}
2023/12/4 13:53
加载中...