日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

codechef Polo the Penguin and the Tree

發(fā)布時(shí)間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 codechef Polo the Penguin and the Tree 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一般xor 的題目都是用trie解決。

那這道題是在樹上的trie;

首先:從root==1,遍歷樹得到1到所有節(jié)點(diǎn)的xor 值。

??????? 然后對(duì)于每個(gè)點(diǎn)我們把其插入二進(jìn)制樹中。

對(duì)于每一個(gè)點(diǎn)查找其二進(jìn)值異或值最大的數(shù) 依次遍歷下來。

注意:邊的數(shù)量開兩倍以上,RE很多次。

find函數(shù)具體是這樣的:

對(duì)于一個(gè)書二進(jìn)值:10001000101

1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<string> 6 #include<cstring> 7 #include<set> 8 #include<map> 9 #include<stdlib.h> 10 11 #define N 223456 12 using namespace std; 13 struct edge 14 { 15 int v,w,next; 16 }e[N]; 17 int tot,nid; 18 int head[N],ans[N]; 19 int next[N*30][2]; 20 void add(int u,int v,int w) 21 { 22 e[tot].v=v; 23 e[tot].w=w; 24 e[tot].next=head[u]; 25 head[u]=tot++; 26 } 27 28 void dfs(int u,int fa) 29 { 30 for (int i=head[u];i!=-1;i=e[i].next) 31 { 32 int v=e[i].v; 33 if (v==fa) continue; 34 ans[v]=ans[u]^e[i].w; 35 dfs(v,u); 36 } 37 } 38 39 void insert(int node,int d,int val) 40 { 41 if (d==30) return; 42 int p=29-d; 43 int c=(val&(1<<p)) ? 1:0; 44 45 if (next[node][c]==-1) next[node][c]=++nid; 46 insert(next[node][c],d+1,val); 47 } 48 49 int solve(int node,int d,int val) 50 { 51 if (d==30) return 0; 52 int p=29-d; 53 int c=(val&(1<<p))?0:1; 54 55 if (next[node][c]!=-1) 56 return (1<<p)+solve(next[node][c],d+1,val); 57 58 return solve(next[node][!c],d+1,val); 59 } 60 61 int main() 62 { 63 int T; 64 scanf("%d",&T); 65 while (T--) 66 { 67 int n; 68 scanf("%d",&n); 69 memset(head,-1,sizeof(head)); 70 memset(next,-1,sizeof(next)); 71 memset(ans,0,sizeof(ans)); 72 tot=nid=0; 73 for (int i=1;i<n;i++) 74 { 75 int u,v,w; 76 scanf("%d%d%d",&u,&v,&w); 77 add(u,v,w); 78 add(v,u,w); 79 } 80 81 dfs(1,-1); 82 for (int i=1;i<=n;i++) insert(0,0,ans[i]); 83 int tmp=0; 84 85 for (int i=1;i<=n;i++) 86 tmp=max(tmp,solve(0,0,ans[i])); 87 printf("%d\n",tmp); 88 } 89 return 0; 90 } View Code

?

我們先要判斷?????????? 01110111010

存在否,這樣才能達(dá)到最大值

?

轉(zhuǎn)載于:https://www.cnblogs.com/forgot93/p/4383735.html

總結(jié)

以上是生活随笔為你收集整理的codechef Polo the Penguin and the Tree的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。