树是一种很常见的数据结构。
我们把N个点,N-1条边的连通无向图称为树。
若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树。
对于两个树T1和T2,如果能够把树T1的所有点重新标号,使得树T1和树T2完全相
同,那么这两个树是同构的。也就是说,它们具有相同的形态。
现在,给你M个有根树,请你把它们按同构关系分成若干个等价类。
树哈希模板题。
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 52
using namespace std;
typedef long long ll;
const int mod=1e9+;
int tt,n,head[N],tot,num[N];
ll disso[N][N],ha[N];
const int base=;
struct node{
int n,to;
}e[N<<];
inline void add(int u,int v){
e[++tot].n=head[u];
e[tot].to=v;
head[u]=tot;
}
void dfs(int u,int fa,int deep){
int ji[N],top=;
ji[++top]=deep;
for(int i=head[u];i;i=e[i].n)if(e[i].to!=fa){
int v=e[i].to;
dfs(v,u,deep+);
ji[++top]=ha[v];
}
ha[u]=;
sort(ji+,ji+top+);
for(int i=;i<=top;++i)ha[u]=(ha[u]*base+ji[i])%mod;
}
int main(){
scanf("%d",&tt);
for(int i=;i<=tt;++i){
scanf("%d",&n);num[i]=n;
memset(head,,sizeof(head));tot=;
int ba;
for(int j=;j<=n;++j){
scanf("%d",&ba);
if(ba)add(ba,j),add(j,ba);
}
for(int j=;j<=n;++j){
dfs(j,,);disso[i][j]=ha[j];
}
sort(disso[i]+,disso[i]+n+);
for(int j=;j<=i;++j){
if(num[j]!=num[i])continue;bool tag=;
for(int k=;k<=n;++k)if(disso[i][k]!=disso[j][k])tag=;
if(!tag){
printf("%d\n",j);
break;
}
}
}
return ;
}

bzoj4337树的同构的更多相关文章

  1. BZOJ4337 树的同构 (树哈希)(未完成)

    样例迷,没过 交了30pts #include <cstdio> #include <iostream> #include <cstring> #include & ...

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

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

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

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

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

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

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

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

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

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

  7. 刷题总结——树的同构(bzoj4337 树上hash)

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

  8. 【BZOJ4337】树的同构(树同构,哈希)

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

  9. 03-树1 树的同构 (C语言链表实现)

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...

随机推荐

  1. connect、resource和dba三种标准角色

    授权语句:grant connect,resource,dba to zwserver 经过授权以后,用户拥有connect.resource和dba三个角色的权限: (1)Connect 角色,是授 ...

  2. checkbox的使用总结,判断是否选中

    方法一: if ($("#checkbox-id").get(0).checked) { // do something } 方法二: if($('#checkbox-id').i ...

  3. 【转帖】介绍 .NET Standard

    [译]介绍 .NET Standard https://zhuanlan.zhihu.com/p/24267356 跟开发争执过 自己不会写代码 的确不好. 若有任何对翻译的建议,烦请指正 有任何问题 ...

  4. LLDB 3.9.1 安装方法

    1. baidu到一个安装方法 进行尝试: 来源: https://zhuanlan.zhihu.com/p/40780819https://www.jianshu.com/p/f965bbba6eb ...

  5. Executor介绍

    1.Executor介绍: Executor是mybatis的核心接口之一,其中定义了数据库操作的基本方法,它的子类结构图如下:这这张关系图中,涉及到了模板方法模式和装饰器模式.BaseExecuto ...

  6. input & collapse & tags

    input & collapse & tags https://ant.design/components/tag-cn/ https://www.iviewui.com/compon ...

  7. 《Tensorflow从入门到精通》

    第一 开发环境搭建 1. tensorflow的环境搭建 windows下安装cpu版tensorflow: pip install tensorflow 在ubuntu上安装gpu版tensorfl ...

  8. pip 升级

    pip install --upgrade qrcode pip install --upgrade qrcode==5.3

  9. [TaskList] 省选前板子补完计划

    省选前本子补完计划 [ ] 带权并查集 [ ] 树上莫队 - UOJ58 [WC2013]糖果公园 loj2485「CEOI2017」Chase

  10. 搭建Hexo博客(一)-创建Hexo环境

    Hexo配合github,可以创建自己的博客.基本原理是使用Hexo生成静态页面,发布到github上.在本地需要搭建Hexo环境. 1.安装nodejs 下载并安装NodeJS,官网地址:https ...