题目链接:http://poj.org/problem?id=3764
题意: 求树上路径边异或和最大值
思路和上一篇一样
只是这个POJ 超内存一直wa也不提示
后来把vector改成 数组模拟之后就过了
代码:
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#define sf scanf
#define pf printf
using namespace std;
using namespace std;
const int maxn = 100000 + 500;
typedef int LL;
int ch[32 * maxn][2];
LL value[32 * maxn];
int node_cnt;
inline void init(){
node_cnt = 1;
memset(ch[0],0,sizeof(ch));
}
inline void Insert(LL x){
int cur = 0;
for(int i = 32;i >= 0;--i){
int idx = (x >> i) & 1;
if(!ch[cur][idx]){
memset(ch[node_cnt],0,sizeof(ch[node_cnt]));
ch[cur][idx] = node_cnt;
value[node_cnt++] = 0;
}
cur = ch[cur][idx];
}
value[cur] = x;
}
inline LL Query(LL x){
int cur = 0;
for(int i = 32;i >= 0;--i){
int idx = (x >> i) & 1;
if(ch[cur][idx ^ 1]) cur = ch[cur][idx ^ 1];
else cur = ch[cur][idx];
}
return value[cur];
}
struct Edge{
int v,next,c;
}edge[maxn * 2];
int head[maxn];
int tot;
void Add_Edge(int u,int v,int w){
edge[tot].v = v;
edge[tot].c = w;
edge[tot].next = head[u];
head[u] = tot++;
}
void init_Adj(){
tot = 0;
memset(head,-1,sizeof(head));
}
int ans;
void DFS(int cur,int cur_xor,int _fa){
ans = max(ans,cur_xor ^ Query(cur_xor));
Insert(cur_xor);
for(int i = head[cur];~i;i = edge[i].next){
if(edge[i].v != _fa){
DFS(edge[i].v,cur_xor ^ edge[i].c,cur);
}
}
}
int main(){
// freopen("read.txt","r",stdin);
int n,u,v,w;
while(~sf("%d",&n)){
init_Adj();
for(int i = 0;i < n - 1;++i){
sf("%d%d%d",&u,&v,&w);
Add_Edge(u,v,w);
Add_Edge(v,u,w);
}
init();
ans = 0;
DFS(0,0,-1);
pf("%d\n",ans);
}
return 0;
}
树路径边异或和最大值

本文介绍了一种解决树上路径边异或和最大值问题的方法,通过使用前缀树来寻找最大异或值,并针对POJ平台的内存限制进行了优化。
309

被折叠的 条评论
为什么被折叠?



