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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HRBUST 2072 树上求最大异或路径值

發布時間:2025/7/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HRBUST 2072 树上求最大异或路径值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個很經典的套路 思想是 F [u,v] = F[1,u] ^ F[1,v]?

這樣就轉化成了n個數兩兩異或 求最大值 可以用字典樹來做 每次用當前數去樹中尋求最大xor值 然后把這個數字插進去 就相當于這個數字和之前所有的數字都比較了

發現自從學習了kdtree等一群數據結構之后...寫什么都想學線段樹的寫法..好像很不錯啊..?

#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #include<map> #include<iostream> #include<string> #include<vector> #include<queue> using namespace std; #define L long long #define pb push_backconst L maxn = 100050 ;L n ; vector<L >q[maxn] ; vector<L >w[maxn] ; L f[maxn] ; bool vis[maxn] ;void bfs(L u) {queue<L > que ;que.push(u) ;que.push(0) ;while(!que.empty()) {L u = que.front() ; que.pop() ;L ff = que.front() ; que.pop() ;for(L i = 0 ; i < q[u].size() ; i ++ ){L v = q[u][i] ;if(vis[v]) {vis[v] = false ;f[v] = ff ^ w[u][i] ;que.push(v) ;que.push(f[v]) ;}}} }L a[50] ; struct node {L l , y ; }b[35 * maxn]; L cnt ; L e[50] ;L did() {L qs = 0 ;L de = 0 ;L res = 0;while(de + 1 <= 32) {L nd = de + 1 ;if(a[nd] == 1) {if(b[qs].l != -1) {res += e[nd] ;qs = b[qs].l ;}else if(b[qs].y != -1) {res += 0 ;qs = b[qs].y ;}else return res ;}else {if(b[qs].y != -1) {res += e[nd] ;qs = b[qs].y ;}else if(b[qs].l != -1) {res += 0 ;qs = b[qs].l ;}else return res ;}de = nd ;}return res ; }void inse() {L qs = 0 ;L de = 0 ;while(de + 1 <= 32) {L nd = de + 1 ;if(a[nd] == 0) {if(b[qs].l == -1) {b[qs].l = (++cnt) ;qs = b[qs].l;b[qs].l = -1 ;b[qs].y = -1 ;}else {qs = b[qs].l ;}}else {if(b[qs].y == -1) {b[qs].y = (++cnt) ;qs = b[qs].y;b[qs].l = -1 ;b[qs].y = -1 ;}else {qs = b[qs].y ;}}de = nd ;} }int main(){while(scanf("%lld",&n) != EOF) {for(L i = 1 ; i <= n ; i ++ )q[i].clear() ;for(L i = 1 ; i <= n ; i ++ )w[i].clear() ;for(L i = 1 ; i < n ; i ++ ){L u , v , c;scanf("%lld%lld%lld",&u,&v,&c) ;q[u].pb(v);q[v].pb(u);w[u].pb(c);w[v].pb(c);}e[32] = 1 ;for(L j = 31 ; j >= 1 ; j -- ) {e[j] = 2*e[j+1] ;}memset(vis,true,sizeof(vis));f[1] = 0 ;vis[1] = false;bfs(1) ;cnt = 0 ;b[cnt].l = -1;b[cnt].y = -1;L ans = 0 ;for(L i = 1 ; i <= n ; i ++ ){L x = f[i];for(L j = 32 ; j >= 1 ; j -- ) {a[j] = x%2 ;x /= 2 ;}L res = did() ;if(ans < res) ans = res ;inse() ;}printf("%lld\n",ans) ;} }

// 好像還是第一次寫字典樹 ..

轉載于:https://www.cnblogs.com/rayrayrainrain/p/6582392.html

總結

以上是生活随笔為你收集整理的HRBUST 2072 树上求最大异或路径值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。