题目链接在这里:P4551 最长异或路径 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

是一道比较经典的问题,对于异或问题经常会使用01trie树来解决。

当然01trie树只是用来统计答案,往往还需要一些预处理操作。

比如此题需要把从当前点到根节点的路径的异或和求出来,讨论与根节点的联系在树的问题中很常见。

 1 #include "bits/stdc++.h"
2 using namespace std;
3 const int MAX=1e6+5;
4 int n;
5 int tot,head[MAX],adj[MAX],nxt[MAX],wei[MAX];
6 int val[MAX];
7 void addedge(int u,int v,int w){
8 tot++;
9 adj[tot]=v;
10 wei[tot]=w;
11 nxt[tot]=head[u];
12 head[u]=tot;
13 }
14 void dfs(int x,int fa){
15 int i,j;
16 for (i=head[x];i;i=nxt[i]){
17 if (adj[i]==fa) continue;
18 val[adj[i]]=val[x]^wei[i];
19 dfs(adj[i],x);
20 }
21 }
22 struct Str{
23 int str[MAX][2],cnt;
24 bool vis[MAX];
25 Str(){
26 memset(str,0,sizeof(str));
27 cnt=0;
28 memset(vis,false,sizeof(vis));
29 }
30 void insert(int x){
31 int i,j,p=0;
32 bool zt;
33 for (i=30;i>=0;i--){
34 zt=(1<<i)&x;
35 if (str[p][zt]==0){
36 str[p][zt]=++cnt;
37 p=cnt;
38 }
39 else p=str[p][zt];
40 }
41 vis[p]=true;
42 }
43 int check(int v){
44 int i,j,p=0,ans=0;
45 bool zt;
46 for (i=30;i>=0;i--){
47 zt=v&(1<<i);
48 if (str[p][!zt]){
49 ans+=(1<<i);
50 p=str[p][!zt];
51 }
52 else p=str[p][zt];
53 // cout<<p<<endl;
54 }
55 // cout<<v<<' '<<ans<<endl;
56 return ans;
57 }
58 }ss;
59 int main(){
60 int i,j,u,v,w,ans=0;
61 scanf("%d",&n);
62 memset(head,0,sizeof(head));
63 memset(val,0,sizeof(val));
64 for (i=1;i<n;i++){
65 scanf("%d%d%d",&u,&v,&w);
66 addedge(u,v,w);
67 addedge(v,u,w);
68 }
69 dfs(1,0);
70 // for (i=1;i<=n;i++)
71 // cout<<i<<" : "<<val[i]<<endl;
72 for (i=1;i<=n;i++)
73 ss.insert(val[i]);
74 for (i=1;i<=n;i++)
75 ans=max(ans,ss.check(val[i]));
76 printf("%d\n",ans);
77 return 0;
78 }

字符串练习2 最长抑或路径(01trie树)的更多相关文章

  1. [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  2. Leetcode 388.文件的最长绝对路径

    文件的最长绝对路径 假设我们以下述方式将我们的文件系统抽象成一个字符串: 字符串 "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 表示: dir ...

  3. Java实现 LeetCode 388 文件的最长绝对路径

    388. 文件的最长绝对路径 假设我们以下述方式将我们的文件系统抽象成一个字符串: 字符串 "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 表示 ...

  4. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  5. 利用Manacher算法寻找字符串中的最长回文序列(palindrome)

    寻找字符串中的最长回文序列和所有回文序列(正向和反向一样的序列,如aba,abba等)算是挺早以前提出的算法问题了,最近再刷Leetcode算法题的时候遇到了一个(题目),所以就顺便写下. 如果用正反 ...

  6. 51nod1274 最长递增路径

    将边排序后dp一下就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<alg ...

  7. 扩展KMP--求字符串S的所有后缀和字符串T的最长公共前缀

    在解上面这个问题前我们要先解决一个类似的问题:求字符串s的所有后缀和s本身的最长公共前缀: 我们用next[]数组保存这些值: 现在我们假设要求next[ x ],并且next[ i ] 0<i ...

  8. POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)

    题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...

  9. [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  10. LeetCode第十四题-字符串数组中最长的共同前缀

    Longest Common Prefix 问题简介: 编写一个函数来查找字符串数组中最长的公共前缀字符串,如果没有公共前缀,则返回空字符串"" 举例: 1: 输入: [“xwq” ...

随机推荐

  1. linux 查看端口号是否对外开放,并开放端口号

    查看对外开放的端口状态查询指定端口是否已开 firewall-cmd --query-port=8780/tcp 提示 yes,表示开启:no表示未开启. 对外开发端口 查看想开的端口是否已开:fir ...

  2. List集合转换成数组

    我现在有个需求:将File集合转换成MultipartFile数组结构 然后我就开始在网上开启了List转换到数组之旅. 首先来看一个例子 ArrayList<String> list=n ...

  3. K8S informer机制

    一.informer介绍 Kubernetes基于声明式API的设计理念,所谓声明式API,即告诉Kubernetes Controller资源对象的期望状态,这样为Kubernetes在事件通知后, ...

  4. nginx文件上传模块 nginx_upload_module

    1.编译安装nginx wget https://github.com/fdintino/nginx-upload-module/archive/refs/heads/master.zip PS:原先 ...

  5. winform 登录后跳转百度地图报错 使用委托解决

    最近用winform做一个登录后跳转到百度地图的小程序,使用了线程,winform的UI是单线程操作的,由于百度地图写在另外一个窗体,导致报错.后来使用了委托解决了这个小问题. delegate vo ...

  6. 'umi' 不是内部或外部命令,也不是可运行的程序 或批处理文件或umi: command not found

    问题 'umi' 不是内部或外部命令,也不是可运行的程序 或批处理文件或umi: command not found 解决方法 参考链接:https://blog.csdn.net/weixin_40 ...

  7. QCheckBox CSS样式

    QCheckBox:!hover { color:white; border-radius:10px; border:1px solid rgb(170, 170, 127); background- ...

  8. 关于promise经典面试题

    这里涉及到同步和异步的问题

  9. element-ui的自定义表头

    自定义表头 需求:之前在做一个项目的时候,原型图要求表头文字需要额外解释就会在文字后面标注 1,2作为上标 html中提供了<sup></sup>和<sub>< ...

  10. vue3封装input组件

    使用了2种方法去封装input组件(.vue与.jsx) 代码如下 父组件: <template> <div> <h1>input组件封装</h1> & ...