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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

刷题总结——xor(ssoj)

發(fā)布時(shí)間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 刷题总结——xor(ssoj) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:

題目背景

OURCE:NOIP2015-SHY-7

題目描述

求一棵帶邊權(quán)的樹(shù)的一條最大?Xor?路徑的值。這里的“路徑”不一定從根到葉子結(jié)點(diǎn),中間一段路徑只要滿足條件也可以。

輸入格式

第一行,一個(gè)整數(shù)?N?,表示一顆樹(shù)有?N?個(gè)節(jié)點(diǎn),接下來(lái)?N-1?行,每行三個(gè)整數(shù)?a,b,c?表示節(jié)點(diǎn)?a?和節(jié)點(diǎn)?b?之間有條權(quán)值為?c?的邊。

輸出格式

輸出僅一行,即所求的最大值。

樣例數(shù)據(jù) 1

輸入  [復(fù)制]

4?
1 2 3?
1 3 4?
1 4 7

輸出

7

備注

【數(shù)據(jù)范圍】
對(duì)?40%?的輸入數(shù)據(jù)?:數(shù)據(jù)退化為一條鏈;
另對(duì)?10%?的輸入數(shù)據(jù)?:N≤1000;
對(duì)?100%?的輸入數(shù)據(jù)?:1≤N≤100000,?c≤231-1。

題解:

套路題

首先預(yù)處理出所有點(diǎn)到根節(jié)點(diǎn)的異或和XOR····然后將其轉(zhuǎn)化成二進(jìn)制從高位到低位儲(chǔ)存到trie樹(shù)上··容易想到兩個(gè)點(diǎn)的XOR的異或和就是這兩個(gè)點(diǎn)的路徑的異或和····所以枚舉每一個(gè)XOR,在trie樹(shù)上走,從高位開(kāi)始查找與起位置上的數(shù)相反的數(shù)是否存在···如果存在優(yōu)先選擇···一邊走一邊計(jì)算答案即可

代碼:

#include<cstdio> #include<cstdlib> #include<cmath> #include<ctime> #include<cctype> #include<string> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int N=1e5+5; const int M=4e6+5; int fst[N],go[N*2],nxt[N*2],val[N*2],tot,n,xo[N],maxx,len; struct node {int son[2]; }tr[M]; inline int R() {char c;int f=0;for(c=getchar();c<'0'||c>'9';c=getchar());for(;c<='9'&&c>='0';c=getchar())f=(f<<3)+(f<<1)+c-'0';return f; } inline void comb(int a,int b,int v) {nxt[++tot]=fst[a],fst[a]=tot,go[tot]=b,val[tot]=v;nxt[++tot]=fst[b],fst[b]=tot,go[tot]=a,val[tot]=v; } inline void dfs(int u,int fa) {for(int e=fst[u];e;e=nxt[e]){int v=go[e];if(v==fa) continue;xo[v]=xo[u]^val[e];dfs(v,u);} } inline void insert(int x) {int po=0; for(int i=len-1;i>=0;i--){int temp=(x>>i)&1;if(!tr[po].son[temp]) tr[po].son[temp]=++tot;po=tr[po].son[temp];} } inline int check(int x) {int ans=x,po=0;for(int i=len-1;i>=0;i--){int temp=(x>>i)&1;if(tr[po].son[temp^1]) ans|=(1<<i),po=tr[po].son[temp^1];else ans^=(temp<<i),po=tr[po].son[temp];}return ans; } int main() {//freopen("a.in","r",stdin);n=R();int a,b,c;for(int i=1;i<n;i++) {a=R(),b=R(),c=R();maxx=max(maxx,c);comb(a,b,c);}dfs(1,0);int temp=maxx;while(temp) temp/=2,len++;tot=0;for(int i=2;i<=n;i++) insert(xo[i]); for(int i=2;i<=n;i++)maxx=max(maxx,check(xo[i]));cout<<maxx<<endl;return 0; }

?

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

總結(jié)

以上是生活随笔為你收集整理的刷题总结——xor(ssoj)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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