【Trie树】POJ 3764 最大连续异或和

【Trie树】POJ 3764 最大连续异或和

题意

已知:给出n个结点的树,定义两结点间的权值为两点之间所有边相异或的值。
求:树中的某两点间的最大权值。

解法

是道好题,在做HDU5845,发现Trie树解决连续异或和问题是很有趣的套路,遂找到这题学习。
//TODO

代码

POJ 数据范围有坑,TLE半天,点开Discuss发现数据范围可能是200000。

//Trie树求最大/最小连续异或和
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAXN = 2e5 + 6;
const int MAXBIT = 30;
struct edge
{
    int nx, v, w;
}e[MAXN << 1];
int last[MAXN];
int etot;
int n;

struct Trie
{
    int child[2];
    void init()
    {
        child[1] = child[0] = -1;
    }
}tr[MAXN << 5];
int cnt;
bool vis[MAXN];
int XOR[MAXN];

void add_edge(int u, int v, int w)
{
    e[etot].nx = last[u];
    e[etot].v = v;
    e[etot].w = w;
    last[u] = etot++;
    e[etot].nx = last[v];
    e[etot].v = u;
    e[etot].w = w;
    last[v] = etot++;
}

void Insert(int x)
{
    int cur = 0;
    for (int i = MAXBIT; i >= 0; --i)
    {
        int k = (x >> i) & 1;
        if (tr[cur].child[k] == -1)
        {
            tr[cur].child[k] = ++cnt;
            tr[cnt].init();
        }
        cur = tr[cur].child[k];
    }
}

int Find(int x)
{
    int cur = 0;
    int ans = 0;
    for (int i = MAXBIT; i >= 0; --i)
    {
        int k = ((x >> i) & 1)? 0 : 1;
        if (tr[cur].child[k] != -1)
        {
            ans |= (1 << i);
            cur = tr[cur].child[k];
        }
        else
            cur = tr[cur].child[1 - k];
    }
    return ans;
}

void dfs(int u)
{
    //Insert(XOR[u]);
    vis[u] = 1;
    for(int k = last[u]; k != -1; k = e[k].nx)
    {
        if (vis[e[k].v]) continue;
        else XOR[e[k].v] = XOR[u] ^ e[k].w;
        dfs(e[k].v);
    }
}

void init()
{
    etot = 0;
    //memset(last, 0xFF, sizeof(last));
    cnt = 0;
    tr[0].init();
    for(int i = 0; i < n; ++i)
    {
        last[i] = -1;
        vis[i] = XOR[i] = 0;
    }
    //memset(vis, 0, sizeof(vis));
    //memset(XOR, 0, sizeof(XOR));
}

int main()
{
    int ans, u, v, w;
    //while(cin>>n)
    while(scanf("%d", &n) == 1)
    {
        init();
        for (int i = 1; i < n; ++i)
        {
            scanf("%d%d%d", &u, &v, &w);
            //cin>>u>>v>>w;
            add_edge(u,v,w);
        }
        dfs(0);
        ans = -1;
        for (int i = 0; i < n; ++i)
        {
            Insert(XOR[i]);
            int t = Find(XOR[i]);
            ans = max(ans, t);
        }
        printf("%d\n", ans);
        //cout<<ans<<endl;
    }

    return 0;
}

参考

http://codecloud.net/86072.html
https://problemsolvingnotes.wordpress.com/2012/02/22/poj-3764-the-xor-longest-path/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给定一棵 $n$ 个节点的二叉,每个节点上有一个整数。求二叉中某个子内的最大和最小数的异或和。 输入格式 第一行包含整数 $n$。 以下 $n$ 行每行描述二叉的一个节点,格式为 ID val lson rson,其中 ID 表示节点编号(范围 1∼n),val 是节点上的整数,lson 和 rson 分别表示该节点的左儿子和右儿子的编号。若节点没有左儿子或右儿子,则对应位置为 0。 输出格式 一个整数,表示异或和。 数据范围 1≤n≤10^5,−10^9≤val≤10^9 输入样例1: 5 1 1 2 3 2 2 4 5 3 3 0 0 4 4 0 0 5 5 0 0 输出样例1: 7 输入样例2: 9 1 1 2 3 2 2 4 5 3 3 6 7 4 4 8 9 5 5 0 0 6 6 0 0 7 7 0 0 8 8 0 0 9 9 0 0 输出样例2: 8 算法 (Trie ,后缀数组,分块) $O(n log n + n \log^2 mod)$ C++ 代码 用Trie实现 ``` #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<stdlib.h> #include<time.h> using namespace std; #define f(a,b,c) for(a=b;a<=c;a++) #define g(a,b,c) for(a=b;a>=c;a--) #define ll long long const ll INF=2e9; const int N=1e5+7,M=1e6+7; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return x*f; } int e[M],ne[M],h[N],idx; void add(int a,int b){e[idx]=b;ne[idx]=h[a];h[a]=idx++;} int fa[N],dep[N],sz[N],son[N],wson[N],dfn[N],dnt,ndfn[N],tp[N]; struct Trie{ int son[2],cnt; }tr[N*32]; int rt[N]; int cnt; void update(int u,int k){ int p=rt[u],q=rt[cnt++]; int i,j; rt[u]=q,tr[q]=tr[p]; f(i,30,0){ tr[q].cnt=tr[p].cnt+1; j=(k>>i)&1; if(tr[p].son[j]==0){ tr[q].son[0]=tr[p].son[0],tr[q].son[1]=tr[p].son[1]; tr[q].son[j]=cnt++,tr[tr[q].son[j]]=(Trie){0,0}; } p=tr[p].son[j],q=tr[q].son[j]; } tr[q].cnt=tr[p].cnt+1; } int query(int u,int v,int k){ int p=rt[u],q=rt[v],res=0; int i,j; f(i,30,0){ j=(k>>i)&1; if(tr[tr[q].son[j^1]].cnt>tr[tr[p].son[j^1]].cnt) res=res|(1<<i),q=tr[q].son[j^1],p=tr[p].son[j^1]; else q=tr[q].son[j],p=tr[p].son[j]; } return res; } void dfs1(int u,int la){ dep[u]=dep[la]+1,fa[u]=la,sz[u]=1; int i,v,maxn=-1; for(i=h[u];~i;i=ne[i]){ v=e[i]; if(v==la) continue; dfs1(v,u); sz[u]+=sz[v]; if(sz[v]>maxn) maxn=sz[v],son[u]=v; } } void dfs2(int u){ int i,v; dfn[u]=++dnt,ndfn[dnt]=u; if(son[u]) wson[son[u]]=dfn[son[u]],tp[son[u]]=tp[u],dfs2(son[u]); else return; for(i=h[u];~i;i=ne[i]){ v=e[i]; if(v==fa[u]||v==son[u]) continue; wson[v]=dfn[v],tp[v]=v,dfs2(v); } } int find(int u,int v){ int f1=tp[u],f2=tp[v]; int ans=0; while(f1!=f2){ if(dep[f1]<dep[f2]) swap(u,v),swap(f1,f2); ans=max(ans,query(wson[f1],wson[u],v)); u=fa[f1],f1=tp[u]; } if(u==v) return ans; if(dep[u]<dep[v]) swap(u,v); return max(ans,query(wson[son[v]],wson[u],v)); } int main(){ int n=read(); memset(h,-1,sizeof h); int i,a,b,c,ans1=0x3f3f3f3f,ans2=-0x3f3f3f3f; f(i,1,n){ a=read(),b=read(),c=read(); if(c){ add(a,c),add(c,a); add(b,c),add(c,b); } else{ add(a,b),add(b,a); } } dfs1(1,0),wson[1]=dfn[1],tp[1]=1,dfs2(1); rt[0]=cnt++,tr[rt[0]]=(Trie){0,0}; f(i,1,n){ a=ndfn[i]; rt[a]=rt[fa[a]]; update(a,read()); } f(i,1,n){ a=ndfn[i]; ans1=min(ans1,query(rt[1],rt[a],read())); ans2=max(ans2,query(rt[1],rt[a],read())); } cout<<ans1+ans2; return 0; } ``` '''

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值