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& ...
随机推荐
- No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type LeadRestControllerTest (e.g. x.new A() where x is an instance of ).
java - No enclosing instance is accessible. Must qualify the allocation with an enclosing instance o ...
- 常见的 CSRF、XSS、sql注入、DDOS流量攻击
CSRF攻击 :跨站请求伪造攻击 ,CSRF全名是Cross-site request forgery,是一种对网站的恶意利用,CSRF比XSS更具危险性 攻击者一般会使用吸引人的图片去引导用户点击进 ...
- ORACLE 当字段中有数据如何修改字段类型
创建视图的时候,因为表太多,里面一些字段类型不一样,PL/SQL报错,为‘表达式必须具有对应表达式相同的数据类型’,发现后,一个字段的类型为CLOB和VARCHAR2(4000)两种,将CLOB进行修 ...
- select非group by字段的方法
只需要将非group by字段放进函数中即可:
- python之路--内置模块02
一. namedtuple 命名元组->类似创建了一个类 from collections import namedtuple # 类 p = namedtuple("Point&qu ...
- 【转】使用 lsof 查找打开的文件
在 UNIX® 环境中,文件无处不在,这便产生了一句格言:“任何事物都是文件”.通过文件不仅仅可以访问常规数据,通常还可以访问网络连接和硬件.在有些情况下,当您使用 ls 请求目录清单时,将出现相 ...
- layer弹层基本参数初尝试
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Java的HashMap数据结构
标题太大~~~自己做点笔记.别人写得太好了. https://www.cnblogs.com/liwei2222/p/8013367.html HashMap 1.6时代, 使用Entry[]数组, ...
- 一、IntelliJ IDEA创建java项目
一.IntelliJ IDEA创建java项目 二.IntelliJ IDEA下载并包含jdbc包 1.下载zip格式的驱动包:https://dev.mysql.com/downloads/conn ...
- 共轭函数Fenchel不等式
f(x)不一定是凸函数,但他的共轭函数一定是凸函数.是仿射函数的逐点上确界. Fenchel不等式 f(x)+f*(x)>=xTy 如