题目链接在这里: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. unity tex2Dlod in vert

    https://forum.unity.com/threads/how-to-sample-a-texture-in-vertex-shader.513816/   GreatWall said: ↑ ...

  2. 整合jUnit4和jUnit5

    整合jUnit4 1.引入依赖 <dependency> <groupId>org.springframework</groupId> <artifactId ...

  3. 访问远程服务-RPC 与 REST

    方法调用 所做的传递参数.传回结果都依赖于栈内存.所以Caller 调用者 Callee 被调用者应该同属一个进程,拥有相同的 栈内存, 进程间通信(Inter-Process Communicati ...

  4. iOS开发 WKWebView实现JS交互

    需求:利用webView加载H5页面,并实现点击H5页面的按钮触发事件,事件是调用OC的方法.简单说就是JS调用OC的方法,这里我们选择的是WKWebView,至于为什么不用UIWebView,自行百 ...

  5. VUE中如何使用MOCK

    安装mockjs npm install mockjs 可以使用数据模板生成模拟数据. Mock.mock( rurl?, rtype?, template ) ) // 或者 Mock.mock( ...

  6. 进程间通信测试-signal

    截图 代码 #include <stdio.h> #include <unistd.h> #include <signal.h> #include <stri ...

  7. DDR内存256M16、512M8含义

    256M16,后面的16是代表数据位数,也可以认为是16个bit.一般一个Byte是8个bit.例如256M4的容量为256*16 bit = 4096Mb = 512MB所以 256M16和 512 ...

  8. B. Going to the Cinema

    https://codeforces.com/contest/1782/problem/B 题目大意就是给定n个人,每个人有一个除自己之外的最少陪同人数,选一部分人去电影院,要求去的人人数大于等于去的 ...

  9. vscode调试openresty

    一.快速上手 1.软件下载 官网地址:https://code.visualstudio.com/ 安装视频:https://code.visualstudio.com/docs/getstarted ...

  10. maven父子项目,由于pom文件中的<packaging>pom</packaging>导致报数据源配置错误

    啊啊啊~~~ 又是踩坑的一天!!! 我在子模块中进行开发,然后子模块的pom文件中也写了<packaging>pom</packaging>导致报数据源配置错误.具体内容如下图 ...