POJ 3764 The xor-longest Path 01字典树 邻接表
2016-08-10 07:14
363 查看
题目链接:http://poj.org/problem?id=3764
题意: 求树上路径边异或和最大值
思路和上一篇一样
只是这个POJ 超内存一直wa也不提示
后来把vector改成 数组模拟之后就过了
代码:
题意: 求树上路径边异或和最大值
思路和上一篇一样
只是这个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 3764 The xor-longest Path (01字典树 + DFS)
- POJ_3764_The xor-longest Path(01字典树)
- POJ 3764 The xor-longest Path ( 字典树应用—— 求连续段相异或最大最小的线性算法)(好题)
- POJ 3764 The xor-longest Path (字典树)
- poj 3764 The xor-longest Path(字典树)
- POJ 3764 The xor-longest Path【字典树】
- POJ 3764 The xor-longest Path 01字典树+dfs
- ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
- POJ 3764 The xor-longest Path(字典树 + 贪心)
- POJ 3764 The xor-longest Path 字典树求最大异或
- POJ 3764 The xor-longest Path 字典树 异或性质
- poj 3764 The xor-longest Path(字典树)
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
- The xor-longest Path ( POJ - 3764)
- [POJ 3764] The xor-longest Path (Tire树 + 贪心)
- poj3764 The xor-longest Path(01字典树+dfs)
- POJ 3764 The xor-longest Path Trie
- POJ 3764 The xor-longest Path (Trie树 + dfs)
- 【POJ 3764】The xor-longest Path 中文题意&题解&代码(C++)
- POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)