样例不对求调一下捏
查看原帖
样例不对求调一下捏
895312
Archy_楼主2024/8/11 12:11
#include <iostream>
#include <cmath>
#include <vector>
#define int long long
#define re register
using namespace std;

const int N = 100005;
int head[N], cnt1, n, rans = -1e9;
int trie[N][2], dis[N], cnt2;

struct node {
	int next, val, to;	
} e[N << 1];

inline void add(int u, int v, int w) {
	e[++cnt1].to = v;
	e[cnt1].val = w; 
	e[cnt1].next = head[u];
	head[u] = cnt1;
}

inline int rd(){
	re int w = 1, x = 0;
	re char c = getchar();
	while(c < 48 || c > 57) {
		if(c == 45) w *= -1;
		c = getchar();
	}
	while(c >= 48 && c <= 57) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return x * w;
}

inline void dfs(int u, int fa) {
	for(re int i = head[u]; i; i = e[i].next) {
		int v = e[i].to;
		if(v == fa) continue;	
		dis[u] = dis[fa] ^ e[i].val;
		dfs(v, u);
	}
}

inline void insert(int x) {
	int now = 1;
	for(re int i = 32; i >= 0; i --) {
		int get = ((x >> i) & 1);
		if(!trie[now][get]) 
				trie[now][get] = ++ cnt2;
		now = trie[now][get];
	}
}

inline int solve(int x) {
	int ans = 0, now = 1;
	for(re int i = 32; i >= 0; i --) {
		int get = ((x >> i) & 1);
		if(trie[now][get ^ 1]) {
			ans += (1 << i);
			now = trie[now][get ^ 1];
		}
		else now = trie[now][get];
	}
	return ans;
}

signed main() {
	n = rd();
	for(re int i = 1; i < n; i ++) {
		int u = rd(), v = rd(), w = rd();
		add(u, v, w); add(v, u, w);
	}
	dfs(1, 0);
	for(re int i = 1; i <= n; i ++) {
		insert(dis[i]);
	}
	for(re int i = 1; i <= n; i ++) {
		rans = max(rans, solve(dis[i]));
	}
	cout << rans;
	return 0;
}

没有更新max值。

2024/8/11 12:11
加载中...