BZOJ 4337

简单记录一种树哈希的方法:以$x$为根的子树的哈希值为$\sum_{y \in son(x)}f_y*base_i$,$f_y$表示以$y$为根的树的哈希值,其中$i$表示$f_y$在若干个儿子中的排名,$base$是$rand$出的对一个质数取模之后的很大的数。

对于本题这样的情况,可以每一个结点都拿出来作为根计算一下,然后再把所有的结果排个序,如果两棵树同构那么排序之后得到的序列一定是一样的。

时间复杂度$O(n^3)$。

Code:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll; const int N = ;
const ll P = 1e9 + ; int n[N], m, tot, head[N];
ll base[N], h[N][N], f[N];
vector <ll> g[N]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} void dfs(int x, int fat) {
g[x].clear();
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x);
g[x].push_back(f[y]);
} if(g[x].empty()) {
f[x] = 1LL;
return;
} f[x] = 0LL;
sort(g[x].begin(), g[x].end());
int vecSiz = g[x].size();
for(int i = ; i < vecSiz; i++)
(f[x] += g[x][i] * base[i + ] % P) %= P;
} inline bool chk(int x, int y) {
for(int i = ; i <= n[x]; i++)
if(h[x][i] != h[y][i]) return ;
return ;
} int main() {
srand();
for(int i = ; i <= ; i++)
base[i] = rand() * rand() % P * rand() % P * rand() % P * rand() % P; read(m);
for(int i = ; i <= m; i++) {
tot = ;
memset(head, , sizeof(head)); read(n[i]);
for(int fa, j = ; j <= n[i]; j++) {
read(fa);
if(fa) add(j, fa), add(fa, j);
} for(int j = ; j <= n[i]; j++) {
dfs(j, );
h[i][j] = f[j];
}
sort(h[i] + , h[i] + n[i] + );
} for(int i = ; i <= m; i++) {
for(int j = ; j <= i; j++) {
if(n[i] != n[j]) continue;
if(chk(i, j)) {
printf("%d\n", j);
break;
}
}
} return ;
}

Luogu 5043 【模板】树同构([BJOI2015]树的同构)的更多相关文章

  1. BZOJ 4337: BJOI2015 树的同构 树hash

    4337: BJOI2015 树的同构 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4337 Description 树是一种很常见的数 ...

  2. bzoj4337: BJOI2015 树的同构 树哈希判同构

    题目链接 bzoj4337: BJOI2015 树的同构 题解 树哈希的一种方法 对于每各节点的哈希值为hash[x] = hash[sonk[x]] * p[k]; p为素数表 代码 #includ ...

  3. [luogu P3384] [模板]树链剖分

    [luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...

  4. 【BZOJ4337】BJOI2015 树的同构 括号序列

    [BZOJ4337]BJOI2015 树的同构 Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱 ...

  5. [BZOJ4337][BJOI2015]树的同构(树的最小表示法)

    4337: BJOI2015 树的同构 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1023  Solved: 436[Submit][Status ...

  6. luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)

    luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...

  7. BZOJ.4337.[BJOI2015]树的同构(树哈希)

    BZOJ 洛谷 \(Description\) 给定\(n\)棵无根树.对每棵树,输出与它同构的树的最小编号. \(n及每棵树的点数\leq 50\). \(Solution\) 对于一棵无根树,它的 ...

  8. BZOJ4337:[BJOI2015]树的同构(树hash)

    Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...

  9. BZOJ4337:[BJOI2015]树的同构——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4337 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根, ...

  10. 【洛谷P3834】(模板)可持久化线段树 1(主席树)

    [模板]可持久化线段树 1(主席树) https://www.luogu.org/problemnew/show/P3834 主席树支持历史查询,空间复杂度为O(nlogn),需要动态开点 本题用一个 ...

随机推荐

  1. SharedPreference作用及数据操作模式

    SharedPreference是Android平台上的一个轻量级的存储类,用来保存应用的一些常用配制,比如Activity状态,Activtiy暂停,将此Activity的状态保存到SharedPr ...

  2. 【学习笔记】Manacher算法

    本文部分图片来源 代码来源(代码是学姐哒.. 一.引入 Manacher算法是用来求最长回文子串的算法,时间复杂度O(n). 回文子串指的是''aacaa'',''noon'',这种正着反着读都一样的 ...

  3. npm 私服工具verdaccio 安装配置试用

      1. 安装 npm install -g verdaccio 2. 启动 verdaccio // 界面显示信息 Verdaccio doesn't need superuser privileg ...

  4. devops 几个方便的工具

    1. fake API      [canned](https://github.com/sideshowcoder/canned  )    fake API.      [wiremock](ht ...

  5. Oracle变量的定义、赋值及使用

    首先我们来看看代码,然后我们在说明和解释代码: declare l_dept ; currtime date := sysdate; l_nam ) :),'yyyymmdd'); -- to_cha ...

  6. 七、Jmeter + ant + jenkins轻量级接口自动化测试

    七.Jmeter + ant + jenkins轻量级接口自动化测试 杀猪不用牛刀,工具没有牛逼高大尚之分,每个工具都有存在的理由:关键是看会不会用,怎么用,有没有用在合适的地方. 需要安装的工具: ...

  7. 配置动态ip为静态ip qq交流总结

    修改 /etc/sysconfig/network-scripts/ifcfg-etho 修改dhcp 为 static 修改后的样例 这三个ip该怎么对应 ifconfig 123各自对应 修改/e ...

  8. shell中的输入输出和编程中的变量(shell 03)

    shell中的输入输出标准输入:键盘标准输出:终端显示器>> 追加是换行追加的echo -n 不尾随换行符 -e 启用解释反斜杠的转义功能 -E 禁用解释反斜杠的转义功能(默认) --he ...

  9. I/O通信模型(BIO,NIO,AIO)

    一.传统的BIO 网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接请 ...

  10. JS面向对象编程,对象,属性,方法。

    document.write('<script type="text/javascript" src="http://api.map.baidu.com/api?v ...