BZOJ4337:[BJOI2015]树的同构——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4337
树是一种很常见的数据结构。我们把N个点,N-1条边的连通无向图称为树。若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树。对于两个树T1和T2,如果能够把树T1的所有点重新标号,使得树T1和树T2完全相同,那么这两个树是同构的。也就是说,它们具有相同的形态。现在,给你M个有根树,请你把它们按同构关系分成若干个等价类。
Q:如何树哈希啊。
A:网上也没有讲解啊,不如看看代码理解一下吧……我就是这么干的。
代码抄自:https://blog.csdn.net/CHNWJD/article/details/78264934当然是没有讲解的。
要注意dfs里的ans一定要有初值!
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const int N=;
const int B=;
const int p[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int to,nxt;
}e[N*];
int n[N],m,cnt,head[N];
ll ha[N][N];
inline void add(int u,int v){
e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt;
}
int dfs(int u,int fa){
ll num[N],ans=B;int tot=;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa)continue;
num[++tot]=dfs(v,u);
}
sort(num+,num+tot+);
for(int i=;i<=tot;i++)ans=ans*B+num[i]*p[i];
return ans;
}
int main(){
m=read();
for(int i=;i<=m;i++){
n[i]=read();cnt=;
memset(head,,sizeof(head));
for(int j=;j<=n[i];j++){
int v=read();
if(v)add(j,v),add(v,j);
}
for(int j=;j<=n[i];j++)ha[i][j]=dfs(j,);
sort(ha[i]+,ha[i]+n[i]+);
bool flag=;
for(int j=;j<=i&&!flag;j++)
if(n[i]==n[j])
for(int k=;k<=n[i];k++){
if(ha[i][k]!=ha[j][k])break;
if(k==n[i]){
printf("%d\n",j);
flag=;
}
}
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
BZOJ4337:[BJOI2015]树的同构——题解的更多相关文章
- bzoj4337: BJOI2015 树的同构 树哈希判同构
题目链接 bzoj4337: BJOI2015 树的同构 题解 树哈希的一种方法 对于每各节点的哈希值为hash[x] = hash[sonk[x]] * p[k]; p为素数表 代码 #includ ...
- [BZOJ4337][BJOI2015]树的同构(树的最小表示法)
4337: BJOI2015 树的同构 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1023 Solved: 436[Submit][Status ...
- BZOJ4337:[BJOI2015]树的同构(树hash)
Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...
- BZOJ4337: BJOI2015 树的同构(hash 树同构)
题意 题目链接 Sol 树的同构问题,直接拿hash判一下,具体流程大概是这样的: 首先转化为有根树,预处理出第\(i\)棵树以\(j\)为根时的hash值. 那么两个树同构当且仅当把两棵树的hash ...
- bzoj4337: BJOI2015 树的同构
hash大法好 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...
- 【BZOJ4337】BJOI2015 树的同构 括号序列
[BZOJ4337]BJOI2015 树的同构 Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱 ...
- BZOJ 4337: BJOI2015 树的同构 树hash
4337: BJOI2015 树的同构 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4337 Description 树是一种很常见的数 ...
- 4337: BJOI2015 树的同构
题解: 树的同构的判定 有根树从根开始进行树hash 先把儿子的f进行排序 $f[i]=\sum_{j=1}^{k} { f[j]*prime[j]} +num[i]$(我没有仔细想这样是不是树是唯一 ...
- [BJOI2015]树的同构
嘟嘟嘟 判断树的同构的方法就是树上哈希. 如果树是一棵有根树,那么只要从根节点出发dfs,每一个节点的哈希值等于按传统方式算出来的子树的哈希值的结果.需要注意的是,算完子树的哈希值后要先排序再加起来, ...
随机推荐
- 初试Docker on Debian on VirtualBox
一直以来都对Docker如雷贯耳,很想尝试一下但都被各种忙给耽误了,最近由于项目调试,需要安装 Oracle 和 SQL Server 数据库,但又不想安装到本机系统里,于是下决心啃一下docker这 ...
- 微信小程序学习笔记(四)
云函数条件查询 exports.main = async (event, context) => { try { return await db.collection('sweething'). ...
- system_Class类说明文档
system_Class类是FastCMS系统必须的,全局对象system是system_Class的实例,其主要包含二类操作: 1.token 操作: token可以存储当前访客的私有信息,取代se ...
- struts2 result type属性说明
首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-type name="chain" ...
- [leetcode-676-Implement Magic Dictionary]
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- Python高级编程-序列化
在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict: dict1 = {'name': 'Rob', 'age': 19, 'score': 90} 可以随时修改变量,比如把age改成 ...
- 蓝牙核心技术概述(四):蓝牙协议规范(HCI、L2CAP、SDP、RFOCMM)(转载)
一.主机控制接口协议 HCI 蓝牙主机-主机控模型 蓝牙软件协议栈堆的数据传输过程: 1.蓝牙控制器接口数据分组:指令分组.事件分组.数据分组(1).指令分组 如:Accpet Connection ...
- Notes of the scrum meeting(12.10)
meeting time:20:00~20:30p.m.,December 10th,2013 meeting place:20号公寓前 attendees: 顾育豪 ...
- 算法与数据结构5.1 Just Sort
★实验任务 给定两个序列 a b,序列 a 原先是一个单调递增的正数序列,但是由于某些 原因,使得序列乱序了,并且一些数丢失了(用 0 表示).经过数据恢复后,找 到了正数序列 b ,且序列 a 中 ...
- 分布式系统理论-terms
Distributed programming is the art of solving the same problem that you can solve on a single comput ...