codechef Polo the Penguin and the Tree
2015-04-01 14:19
453 查看
一般xor 的题目都是用trie解决。
那这道题是在树上的trie;
首先:从root==1,遍历树得到1到所有节点的xor 值。
然后对于每个点我们把其插入二进制树中。
对于每一个点查找其二进值异或值最大的数 依次遍历下来。
注意:边的数量开两倍以上,RE很多次。
find函数具体是这样的:
对于一个书二进值:10001000101
View Code
我们先要判断 01110111010
存在否,这样才能达到最大值
那这道题是在树上的trie;
首先:从root==1,遍历树得到1到所有节点的xor 值。
然后对于每个点我们把其插入二进制树中。
对于每一个点查找其二进值异或值最大的数 依次遍历下来。
注意:边的数量开两倍以上,RE很多次。
find函数具体是这样的:
对于一个书二进值:10001000101
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<string> #include<cstring> #include<set> #include<map> #include<stdlib.h> #define N 223456 using namespace std; struct edge { int v,w,next; }e ; int tot,nid; int head ,ans ; int next[N*30][2]; void add(int u,int v,int w) { e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; head[u]=tot++; } void dfs(int u,int fa) { for (int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; if (v==fa) continue; ans[v]=ans[u]^e[i].w; dfs(v,u); } } void insert(int node,int d,int val) { if (d==30) return; int p=29-d; int c=(val&(1<<p)) ? 1:0; if (next[node][c]==-1) next[node][c]=++nid; insert(next[node][c],d+1,val); } int solve(int node,int d,int val) { if (d==30) return 0; int p=29-d; int c=(val&(1<<p))?0:1; if (next[node][c]!=-1) return (1<<p)+solve(next[node][c],d+1,val); return solve(next[node][!c],d+1,val); } int main() { int T; scanf("%d",&T); while (T--) { int n; scanf("%d",&n); memset(head,-1,sizeof(head)); memset(next,-1,sizeof(next)); memset(ans,0,sizeof(ans)); tot=nid=0; for (int i=1;i<n;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } dfs(1,-1); for (int i=1;i<=n;i++) insert(0,0,ans[i]); int tmp=0; for (int i=1;i<=n;i++) tmp=max(tmp,solve(0,0,ans[i])); printf("%d\n",tmp); } return 0; }
View Code
我们先要判断 01110111010
存在否,这样才能达到最大值
相关文章推荐
- Codeforces Polo the Penguin and Matrix
- codeforces#289_B Polo the Penguin and Matrix
- codeforces#289_B Polo the Penguin and Matrix
- Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)
- [主席树维护HASH && SET维护DFS序] Codechef. Walks on the binary tree
- A. Polo the Penguin and Segments
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- CUIT ACM Personal Training 11.27(FM)F - Polo the Penguin and Strings
- codeforces 289 B. Polo the Penguin and Matrix
- codechef Tree and Queries Solved
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codechef Wonder Woman and the Dark man
- Codeforces-289a I Polo the Penguin and Segments
- codeforces 289 B. Polo the Penguin and Matrix
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
- CodeForces 289A Polo the Penguin and Segments
- A. Polo the Penguin and Strings
- A. Polo the Penguin and Strings
- Codeforces Round #430 C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs