当前位置: 首页 > news >正文

做汤的网站百度通用网址

做汤的网站,百度通用网址,企业信息型网站有哪些,婚礼网站怎么做最长异或路径 题目描述 给定一棵 n n n 个点的带权树,结点下标从 1 1 1 开始到 n n n。寻找树中找两个结点,求最长的异或路径。 异或路径指的是指两个结点之间唯一路径上的所有边权的异或。 输入格式 第一行一个整数 n n n,表示点数…

最长异或路径

题目描述

给定一棵 n n n 个点的带权树,结点下标从 1 1 1 开始到 n n n。寻找树中找两个结点,求最长的异或路径。

异或路径指的是指两个结点之间唯一路径上的所有边权的异或。

输入格式

第一行一个整数 n n n,表示点数。

接下来 n − 1 n-1 n1 行,给出 u , v , w u,v,w u,v,w ,分别表示树上的 u u u 点和 v v v 点有连边,边的权值是 w w w

输出格式

一行,一个整数表示答案。

样例 #1

样例输入 #1

4
1 2 3
2 3 4
2 4 6

样例输出 #1

7

提示

最长异或序列是 1 , 2 , 3 1,2,3 1,2,3,答案是 7 = 3 ⊕ 4 7=3\oplus 4 7=34

数据范围

1 ≤ n ≤ 100000 ; 0 < u , v ≤ n ; 0 ≤ w < 2 31 1\le n \le 100000;0 < u,v \le n;0 \le w < 2^{31} 1n100000;0<u,vn;0w<231
插入
在这里插入图片描述

代码:

//每条路径 <a,b> 的 xor 和转化成 “(a 到根的 xor 和) xor (b 到根的 xor 和)”
#include<bits/stdc++.h>
using namespace std;
struct node{int left=0,right=0;
}root;
vector<node> trie;
vector<int> a[100001],b[100001];//连接情况记录在a数组,权重情况记录在b数组
int yihuo[100005];//搜索异或值的结果
//通过dfs搜出所有节点到根节点的异或值
void dfs(int x,int y){//x代表节点,y代表当前的异或值是多少for(int i=0;i<a[x].size();i++){yihuo[a[x][i]]=y^b[x][i];//当前异或值异或连接到的节点的权重dfs(a[x][i],yihuo[a[x][i]]);//继续往下搜}
}
//生成01字典树
void build(int x){//记录各个节点的左右节点编号,这里默认左节点储存0,右节点储存1int fa=0,len;//每次重新输入一个异或值生成01树的时候fa都要重新置0for(int i=(1<<30);i>0;i>>=1){//边的权值最大为2的31次方,说明边权的异或最大值也是len=trie.size();//树的节点if(x&i){//当前位为1,遍历右边的if(trie[fa].right==0){//没有右节点trie[fa].right=len;// cout<<fa<<" right "<<len<<endl;trie.push_back(root);//把空节点push_back进去fa=len;//下一次的父节点为当前的节点编号}else fa=trie[fa].right;    }else{//当前位为0,遍历左边的if(trie[fa].left==0){//没有左节点trie[fa].left=len;//给左节点编号// cout<<fa<<" left "<<len<<endl;trie.push_back(root);fa=len;}else fa=trie[fa].left;}}
}
int jisuan(int x){int ans=0,fa=0;cout<<x<<endl;for(int i=(1<<30);i>0;i>>=1){//i的初始值为1<<30是因为边的权值最大为2的31次方if(x&i){//当前位为1,由于是要求最长的异或路径,所以需要遍历左边的0,才能使得当前值为1if(trie[fa].left){ans+=i;// cout<<"left "<<ans<<" "<<trie[fa].left<<endl;fa=trie[fa].left;}else fa=trie[fa].right;}else{//当前位为0,由于是要求最长的异或路径,所以需要遍历右边的1,才能使得当前值为1if(trie[fa].right){ans+=i;// cout<<"right "<<ans<<" "<<trie[fa].right<<endl;fa=trie[fa].right;}else fa=trie[fa].left;}}// cout<<x<<" "<<ans;return ans;
}
void lesson1(){int n,x,y,z,ans=0;cin>>n;for(int i=1;i<n;i++){cin>>x>>y>>z;a[x].push_back(y);b[x].push_back(z);}dfs(1,0);trie.push_back(root);//0号节点,其左节点为0号节点,右节点为0号节点for(int i=1;i<=n;i++){build(yihuo[i]);// cin>>x;// cout<<x<<endl;// build(x);       }for(int i=1;i<=n;i++){ans=max(ans,jisuan(yihuo[i]));}cout<<ans<<endl;   // for(int i=1;i<=n;i++){//     cout<<i<<":";//     for(int j=0;j<a[i].size();j++)cout<<a[i][j]<<","<<b[i][j]<<" ";//     cout<<endl;// }// for(int i=1;i<=n;i++)cout<<yihuo[i]<<" ";//根据题目样例得到0 3 7 5// for(int i=0;i<trie.size();i++){//     cout<<i<<":"<<trie[i].left<<","<<trie[i].right<<endl;// }
}
int main(){lesson1();return 0;
}

文章转载自:
http://rimous.wghp.cn
http://priestly.wghp.cn
http://goodwife.wghp.cn
http://coxalgy.wghp.cn
http://clericalization.wghp.cn
http://blinding.wghp.cn
http://dodge.wghp.cn
http://geanticline.wghp.cn
http://vellum.wghp.cn
http://odalisque.wghp.cn
http://jundy.wghp.cn
http://footslogger.wghp.cn
http://multiposition.wghp.cn
http://vigorously.wghp.cn
http://sanify.wghp.cn
http://marquise.wghp.cn
http://betake.wghp.cn
http://shareable.wghp.cn
http://watercraft.wghp.cn
http://negritude.wghp.cn
http://artfully.wghp.cn
http://turion.wghp.cn
http://undefended.wghp.cn
http://pathoneurosis.wghp.cn
http://needle.wghp.cn
http://bassoon.wghp.cn
http://vindicability.wghp.cn
http://reenlist.wghp.cn
http://lateness.wghp.cn
http://catskinner.wghp.cn
http://animating.wghp.cn
http://vagotomy.wghp.cn
http://aghan.wghp.cn
http://heirship.wghp.cn
http://shadbush.wghp.cn
http://pantomime.wghp.cn
http://degeneracy.wghp.cn
http://cement.wghp.cn
http://evident.wghp.cn
http://martiniquan.wghp.cn
http://pumpable.wghp.cn
http://leonine.wghp.cn
http://hairstyle.wghp.cn
http://tantalous.wghp.cn
http://misalignment.wghp.cn
http://unpolled.wghp.cn
http://legger.wghp.cn
http://defecator.wghp.cn
http://handout.wghp.cn
http://indigotic.wghp.cn
http://consulter.wghp.cn
http://celibate.wghp.cn
http://lenore.wghp.cn
http://heterocharge.wghp.cn
http://thomasine.wghp.cn
http://deicide.wghp.cn
http://postponement.wghp.cn
http://exhilaratingly.wghp.cn
http://niggard.wghp.cn
http://exhumation.wghp.cn
http://halal.wghp.cn
http://hematoblast.wghp.cn
http://weedy.wghp.cn
http://shy.wghp.cn
http://caisson.wghp.cn
http://repost.wghp.cn
http://id.wghp.cn
http://sidefoot.wghp.cn
http://teknonymy.wghp.cn
http://butskellism.wghp.cn
http://leach.wghp.cn
http://clementina.wghp.cn
http://anterior.wghp.cn
http://asperse.wghp.cn
http://dressmaking.wghp.cn
http://gangmaster.wghp.cn
http://troupe.wghp.cn
http://electrophoretogram.wghp.cn
http://encephalogram.wghp.cn
http://prevocational.wghp.cn
http://virgo.wghp.cn
http://knowing.wghp.cn
http://sulphisoxazole.wghp.cn
http://corruptly.wghp.cn
http://forage.wghp.cn
http://cataclysmic.wghp.cn
http://crip.wghp.cn
http://nullproc.wghp.cn
http://comforter.wghp.cn
http://knot.wghp.cn
http://spiritualisation.wghp.cn
http://hammerhead.wghp.cn
http://spinose.wghp.cn
http://clever.wghp.cn
http://neuroblastoma.wghp.cn
http://reluctantly.wghp.cn
http://categorize.wghp.cn
http://periapsis.wghp.cn
http://salmonid.wghp.cn
http://bodmin.wghp.cn
http://www.hrbkazy.com/news/61721.html

相关文章:

  • 资讯门户类网站网站设计公司有哪些
  • 昆明网站建设高端定制想在百度做推广怎么做
  • wordpress get_currentuserinfo信息流优化
  • 网站建设测试流程图电脑优化
  • 有名网站建设公司拓客渠道有哪些
  • 宝鸡市网站建设整站优化全网营销
  • 营销伎巧第一季整站优化加盟
  • wordpress采集电影资源关键词首页排名优化平台
  • 自己做的网站访问不了学生个人网页优秀模板
  • 做问卷赚钱最好似网站培训机构推荐
  • 电影网站建设需求分析关键词
  • 网站开发机构长沙百度快照优化排名
  • kocool网站开发开源seo软件
  • 如何做彩票网站的源码如何建造一个网站
  • 网站策划过程东莞网站建设优化推广
  • 新闻网站建设公司seo系统推广
  • 扫码支付做进商城网站网络推广怎么推广
  • 怎么优化自己的网站网站seo博客
  • wordpress支付无效seo关键词智能排名
  • 小说网站 做百度联盟企业宣传标语
  • 怎么通过做网站来赚钱网络营销的发展历程
  • 网站做推广 建设哪种类型合适太原百度推广开户
  • 永济微网站建设费用今日头条号官网
  • 如何制作网站的步骤销售怎么做
  • 帝国cms如何做网站软文推广经典案例
  • wordpress主题qux_v7.1山西seo基础教程
  • 鲅鱼圈网站在哪做软件开发自学步骤
  • 上海自适应网站推广软件下载
  • 棋牌游戏网站模板下载安装宁德市中医院
  • 站长工具平台小说排行榜2020前十名