我的天。。普及组这么$hard$。。。

然后好像没有人用我的垃圾做法,,,好像是$O(n)$,但十分的慢,并且极其暴力$qwq$

具体来说,就是直接$dfs$求出树高,然后想像出把原来的树补成满二叉树的形态

$like\space this:$

震不震惊$qwq$

然后对子树哈希,同时保存正向的哈希值$h1[u]$和反向的哈希值$h2[u]$(对称时用)。
但每次向上合并时要乘的是$Base^{sz+0/1}$,其中$sz=$子树所形成的完全二叉树的大小。

这样哈希值既可以表示点位置(不同的位置点在完全二叉树中的位置不同),又可以表示点的数值。

如果还不懂可以康康代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define R register int
using namespace std;
#define ull unsigned long long
#define ll long long
#define pause (for(R i=1;i<=10000000000;++i))
#define IN freopen("NOIPAK++.in","r",stdin)
#define OUT freopen("out.out","w",stdout)
namespace Fread {
static char B[<<],*S=B,*D=B;
#ifndef JACK
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
#endif
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
if(ch==EOF) return EOF; do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
} inline bool isempty(const char& ch) {return (ch<=||ch>=);}
inline void gs(char* s) {
register char ch; while(isempty(ch=getchar()));
do *s++=ch; while(!isempty(ch=getchar()));
}
}using Fread::g; using Fread::gs;
const int N=,B=;
int ch[N][];
#define ls ch[u][0]
#define rs ch[u][1]
int n,w[N],sz[N],d[N],ans,mxd;
ull h1[N],h2[N],p[N],tmp;
inline void dfs1(int u) { mxd=max(d[u],mxd);
if(~ls) d[ls]=d[u]+,dfs1(ls); if(~rs) d[rs]=d[u]+,dfs1(rs);
}
inline void dfs(int u) {sz[u]=;
if(~ls) dfs(ls),sz[u]+=sz[ls]; if(~rs) dfs(rs),sz[u]+=sz[rs];
if(~ls&&~rs&&sz[ls]==sz[rs]&&h1[ls]==h2[rs]&&h2[ls]==h1[rs]) ans=max(ans,sz[u]);
if(!~ls&&!~rs) h1[u]=h2[u]=w[u],ans=max(ans,); else if(~ls&&!~rs) h1[u]=h1[ls]*p[d[u]]+w[u]*(p[d[u]]-),h2[u]=h2[ls]+w[u]*(p[d[u]]-);
else if(!~ls&&~rs) h1[u]=w[u]*(p[d[u]]-)+h1[rs],h2[u]=w[u]*(p[d[u]]-)+h2[rs]*p[d[u]];
else h1[u]=h1[ls]*p[d[u]]+w[u]*(p[d[u]]-)+h1[rs],h2[u]=h2[ls]+w[u]*(p[d[u]]-)+h2[rs]*p[d[u]];
}
signed main() {
#ifdef JACK
IN;
#endif
n=g(); for(R i=;i<=n;++i) w[i]=g(); for(R u=;u<=n;++u) ls=g(),rs=g();
d[]=; dfs1();
for(R i=;i<=n;++i) d[i]=mxd-d[i]; p[]=; tmp=p[]=B; for(R i=;i<=mxd;++i) p[i+]=(tmp*=tmp);
dfs(); printf("%d\n",ans);
}

2019.07.08/09

最新文章

  1. 完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
  2. lecture13-BP算法的讨论和置信网
  3. Linux 利器- Python 脚本编程入门(一)
  4. 如何在树莓派上运行雷神之锤III
  5. HDU 2897
  6. CSS3之动画相关
  7. Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid
  8. Andriod中绘(画)图----Canvas的使用具体解释
  9. 游戏排行榜-Python实现
  10. RTL-SDR简单介绍
  11. Android 联网监控抓包工具的制作(tcpdump的使用)
  12. Laptop Ubuntu16.04/14.04 安装Nvidia显卡驱动
  13. win10- *.msi 软件的安装,比如 SVN安装报2503,2502
  14. android动画的实现过程
  15. Go中原始套接字的深度实践
  16. 课下必做MyCP
  17. linux下python3(Setup)项目
  18. 设计模式C++学习笔记之十二(Command命令模式)
  19. unorder_map 自定义KEY
  20. JBMP学习引导

热门文章

  1. DB2部分查询SQL
  2. php实现算法
  3. 关于BIOS系统的认识和学习(源自摘录)
  4. 牛客 133D 挑选队友 (分治FFT)
  5. 训练技巧详解【含有部分代码】Bag of Tricks for Image Classification with Convolutional Neural Networks
  6. Asp.net core 学习笔记 ef core Surrogate Key, Natural Key, Alternate Keys
  7. C# 32位系统与64位系统调用不同的DLL文件
  8. c# http文件上传
  9. springboot-异步、发送邮件(二)
  10. gitlab 搭建