博客
关于我
Luogu P4551 最长异或路径 (字符串,01Trie)
阅读量:231 次
发布时间:2019-03-01

本文共 1366 字,大约阅读时间需要 4 分钟。

题目链接:

题意:给定n个点(1<=n<=1e5),n-1条带权无向边 u,v,w。求最大的异或路径,即所有最短路径中的异或和的最大值。

题解:01Trie模板题

1.求num[i]:首先在原树上随便选择一个点作为根节点s,然后num[i]表示表示根节点到点i的路径异或和,那么任意两点j,k之间的路径异或和为num[j]^num[k]。

2.利用num[i]建立二进制Trie树:优化空间,空间复杂度降到大概O(n*30)。

3.求最大路径异或和:ans=max(ans,query(num[i]))。query返回的是num[i]与某一个num[j]的最大值,一次查询时间复杂度大概只需要O(30)。具体操作见代码

总结:这题是很模板的01Trie,提供了新的解题思路。

算法学习重深度还是广度。我的选择是:能深度尽量深度,疲惫之后就广度,把字符串的只是全部联结起来,然后不断突破自己即可。

好好总结一下模板!

代码:

#include 
#define ll long long#define pi acos(-1)#define pb push_back#define mst(a, i) memset(a, i, sizeof(a))#define pll pair
#define fi first#define se second#define mp(x,y) make_pair(x,y)#define rep(i,a,n) for(ll i=a;i<=n;i++)#define per(i,n,a) for(ll i=n;i>=a;i--)#define dbg(x) cout << #x << "===" << x << endl#define dbgg(l,r,x) for(ll i=l;i<=r;i++) cout<
<<" ";cout<<"<<<"<<#x;cout<
void read(T &x){T res=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){res=(res<<3)+(res<<1)+c-'0';c=getchar();}x=res*f;}void print(ll x){if(x<0){putchar('-');x=-x;}if(x>9)print(x/10);putchar(x%10+'0');}const ll maxn = 1e5 + 10;const ll mod = 1e9+7;ll n,u,v,w;ll num[maxn];struct Trie_01{ ll trie[maxn*30][2],val[maxn*30],cnt;//注意cnt的最大值 vector
g[maxn]; //01Trie插入数 void insert(ll s){ ll u=0; for(ll i=30;i>=0;i--){ ll t=1<
=0;i--){ ll t=1<
>>"<
<<" "<
<

 

转载地址:http://lrst.baihongyu.com/

你可能感兴趣的文章
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>