bzoj4337树的同构
#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树的同构的更多相关文章
- BZOJ4337 树的同构 (树哈希)(未完成)
样例迷,没过 交了30pts #include <cstdio> #include <iostream> #include <cstring> #include & ...
- bzoj4337: BJOI2015 树的同构 树哈希判同构
题目链接 bzoj4337: BJOI2015 树的同构 题解 树哈希的一种方法 对于每各节点的哈希值为hash[x] = hash[sonk[x]] * p[k]; p为素数表 代码 #includ ...
- 【BZOJ4337】BJOI2015 树的同构 括号序列
[BZOJ4337]BJOI2015 树的同构 Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱 ...
- [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]树的同构——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4337 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根, ...
- 刷题总结——树的同构(bzoj4337 树上hash)
Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...
- 【BZOJ4337】树的同构(树同构,哈希)
题意: 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如果能够把树T1T ...
- 03-树1 树的同构 (C语言链表实现)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...
随机推荐
- connect、resource和dba三种标准角色
授权语句:grant connect,resource,dba to zwserver 经过授权以后,用户拥有connect.resource和dba三个角色的权限: (1)Connect 角色,是授 ...
- checkbox的使用总结,判断是否选中
方法一: if ($("#checkbox-id").get(0).checked) { // do something } 方法二: if($('#checkbox-id').i ...
- 【转帖】介绍 .NET Standard
[译]介绍 .NET Standard https://zhuanlan.zhihu.com/p/24267356 跟开发争执过 自己不会写代码 的确不好. 若有任何对翻译的建议,烦请指正 有任何问题 ...
- LLDB 3.9.1 安装方法
1. baidu到一个安装方法 进行尝试: 来源: https://zhuanlan.zhihu.com/p/40780819https://www.jianshu.com/p/f965bbba6eb ...
- Executor介绍
1.Executor介绍: Executor是mybatis的核心接口之一,其中定义了数据库操作的基本方法,它的子类结构图如下:这这张关系图中,涉及到了模板方法模式和装饰器模式.BaseExecuto ...
- input & collapse & tags
input & collapse & tags https://ant.design/components/tag-cn/ https://www.iviewui.com/compon ...
- 《Tensorflow从入门到精通》
第一 开发环境搭建 1. tensorflow的环境搭建 windows下安装cpu版tensorflow: pip install tensorflow 在ubuntu上安装gpu版tensorfl ...
- pip 升级
pip install --upgrade qrcode pip install --upgrade qrcode==5.3
- [TaskList] 省选前板子补完计划
省选前本子补完计划 [ ] 带权并查集 [ ] 树上莫队 - UOJ58 [WC2013]糖果公园 loj2485「CEOI2017」Chase
- 搭建Hexo博客(一)-创建Hexo环境
Hexo配合github,可以创建自己的博客.基本原理是使用Hexo生成静态页面,发布到github上.在本地需要搭建Hexo环境. 1.安装nodejs 下载并安装NodeJS,官网地址:https ...