题目链接在这里: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. 《SAP MDM主数据管理》.pdf

    <SAP MDM主数据管理>.pdf 有需要的联系 wx :erpworld

  2. SFINAE几种实现方式

    一.通过函数返回值实现 template<class T> typename std::enable_if<std::is_trivially_default_constructib ...

  3. 苹果Macbook快捷键使用大全

    苹果电脑Macbook快捷键如何使用?很多小伙伴经常询问小编有关于苹果电脑的相关问题,其实很多快捷键都可以提高生产力,那么接下来一起看看苹果Macbook快捷键使用大全! 苹果电脑Macbook快捷键 ...

  4. TypeScript Array(数组)

    TypeScript Array(数组) 数组对象是使用单独的变量名来存储一系列的值. 数组非常常用. 假如你有一组数据(例如:网站名字),存在单独变量如下所示: var site1="Go ...

  5. MySQL 利用时间(秒分时日月年)分组统计

    1.统计 七天 前 人数 select count(*) from your_table where last_login_time> date_sub(date(now()), interva ...

  6. Token过期导致页面多个请求报错提示多次

    关于Token过期导致页面多个请求报错提示的问题 我们先在全局定义一个变量(global.js)来控制token是否过期 export default { // token无效标记 TokenInva ...

  7. vue3 微信支付和支付宝支付 H5和微信内置浏览器

    vue3微信支付和支付宝支付 // 判断是否为微信内置浏览器 let browser: any = navigator.userAgent.toLowerCase(); let isWechat: a ...

  8. fork子进程父进程死掉之后,getppid()不为1的解决办法

    代码例子:程序在执行之后,会一直死在while中,打印发现当父进程被终止,getppid() 的值也不为1 pid_t pid;if((pid = fork()) < 0){ printf(&q ...

  9. 打开part文件夹

    1 #include <uf.h> 2 #include <uf_ui.h> 3 #include <uf_part.h> 4 #include <atlst ...

  10. CVPixelBufferRef

    在iOS里,我们经常能看到 CVPixelBufferRef 这个类型,在Camera 采集返回的数据里得到一个CMSampleBufferRef,而每个CMSampleBufferRef里则包含一个 ...