POJ 3764 树上XOR 贪心+字典树
来源:互联网 发布:淘宝店铺装修素菜 编辑:程序博客网 时间:2024/05/16 14:06
题意:已知:给出n个结点,n-1条边(附边的长度).定义:两结点间的异或长度为两点之间所有边相异或的值.求:树中的最长异或长度.
易知道先遍历出N个点的XOR值,接着就变成N个XOR里选两个使得XOR值最大~~
这里直接用贪心+字典树进行求解~~
将每一个XOR转换成二进制,从高往低插入到字典树
查询时,每次尽量往不同的方向前进,如果这个不同方向没有的话,那只好按照原来的顺序前进了~~~肯定可以前行
#include <algorithm>#include <iostream>#include<string.h>#include <fstream>#include <math.h>#include <vector>#include <cstdio>#include <string>#include <queue>#include <stack>#include <map>#include <set>#define exp 1e-8#define fi first#define ll long long#define INF 0x3f3f3f3f3f3f3f3f#define pb(a) push_back(a)#define mp(a,b) make_pair(a,b)#define all(a) a.begin(),a.end()#define mm(a,b) memset(a,b,sizeof(a));#define for0(a,b) for(int a=0;a<=b;a++)//0---(b-1)#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c#define repp(a,b,c)for(int a=b;a>=c;a--)///#define cnt_one(i) __builtin_popcount(i)#define stl(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)using namespace std;void bug(string m="here"){cout<<m<<endl;}template<typename __ll> inline void READ(__ll &m){__ll 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*10+ch-'0';ch=getchar();}m=x*f;}template<typename __ll>inline void read(__ll &m){READ(m);}template<typename __ll>inline void read(__ll &m,__ll &a){READ(m);READ(a);}template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c,__ll &d){READ(m);READ(a);READ(b);READ(c);read(d);}template < class T > inline void out(T a){if(a<0){putchar('-');a=-a;}if(a>9)out(a/10);putchar(a%10+'0');}template < class T > inline void outln(T a){out(a);puts("");}template < class T > inline void out(T a,T b){out(a);putchar(' ');out(b);}template < class T > inline void outln(T a,T b){out(a);putchar(' ');outln(b);}template < class T > inline void out(T a,T b,T c){out(a);putchar(' ');out(b);putchar(' ');out(c);}template < class T > inline void outln(T a,T b,T c){out(a);putchar(' ');outln(b);putchar(' ');outln(b);}template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }const int cnt_edge=200020; //修改啊const int cnt_v=100100;int head[cnt_v],cnt_e;struct EDGE{int u,v,next,cost;}edge[cnt_edge];void addedge(int u,int v,int cost=0){edge[cnt_e].u=u;edge[cnt_e].v=v;edge[cnt_e].cost=cost;edge[cnt_e].next=head[u];head[u]=cnt_e++;}void init(){cnt_e=0;memset(head,-1,sizeof(head));}#define erg(i,u) for(int i=head[u];i!=-1;i=edge[i].next)const int maxn=100100;struct tree{ int idx; int son[2]; void clear(){ idx=0; son[0]=son[1]=-1; }}tree[maxn*32];int cnt,n;int val[maxn];int XOR[maxn];void dfs(int u,int fa,int sum){ XOR[u]=sum; erg(i,u) { int v=edge[i].v; if(v==fa)continue; dfs(v,u,sum^edge[i].cost); }}void insert(int idx,int num){ int cur=0; for(int i=31;i>=0;i--) { int tmp=(num>>i)&1; if(tree[cur].son[tmp]==-1) { tree[cur].son[tmp]=cnt; tree[cnt++].clear(); } cur=tree[cur].son[tmp]; } tree[cur].idx=idx;}int query(int idx,int num){ int cur=0; for(int i=31;i>=0;i--) { int tmp=(num>>i)&1; if(tree[cur].son[tmp^1]!=-1) cur=tree[cur].son[tmp^1]; else cur=tree[cur].son[tmp]; } return XOR[idx]^(XOR[tree[cur].idx]);}int main(){ while(scanf("%d",&n)!=EOF) { cnt=1; init(); tree[0].clear(); for1(i,n-1) { int a,b,c; read(a,b,c); a++,b++; addedge(a,b,c); addedge(b,a,c); } dfs(1,0,0); int ans=0; for(int i=1;i<=n;i++) { if(i>1)ans=max(ans,query(i,XOR[i])); insert(i,XOR[i]); } outln(ans); }}
0 0
- POJ 3764 树上XOR 贪心+字典树
- POJ 3764 The xor-longest Path(字典树 + 贪心)
- 【Poj 3764】The xor-longest Path 字典序+贪心
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
- Xor Sum(字典树加贪心)
- Hdu 4825 Xor Sum【字典树+贪心】
- POJ 3764 The xor-longest Path Trie树/字典树
- poj 3764 The xor-longest Path(字典树)
- POJ 3764 The xor-longest Path【字典树】
- POJ 3764 The xor-longest Path (01字典树 + DFS)
- POJ 3764 The xor-longest Path 01字典树+dfs
- poj 3764 字典树 树上任意两点边权异或最大值
- [POJ 3764] The xor-longest Path (Tire树 + 贪心)
- POJ 3764 The xor-longest Path trie树+贪心
- POJ 4825 Xor Sum 字典树
- HDU 4825:Xor Sum(01字典树+贪心)
- POJ 3764 The xor-longest Path 字典树 异或性质
- POJ 3764 The xor-longest Path 字典树求最大异或
- 100个iOS开发/设计面试题汇总
- SpringMVC 文件上传下载
- 关于编程的杂散积累
- Python - OS模块常用方法
- 151031的测试总结
- POJ 3764 树上XOR 贪心+字典树
- 30.Android v4 LruCache
- 关于IE浏览器加载外部js注意事项
- 【VS开发】VS2010 MFC中控件、对话框等背景颜色动态修改的方法
- 【UI】锤子手机-坚果手机-文艺青年版-配色色号
- iOS 【通过sb加载描述window根控制器的view】
- hibernate中三种状态
- 10.31
- git使用之技巧