BZOJ4337:[BJOI2015]树的同构(树hash)
Description
Input
Output
Sample Input
4 0 1 1 2
4 2 0 2 3
4 0 1 1 1
4 0 1 2 3
Sample Output
1
3
1
HINT
Solution
Code
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#define LL long long
#define MOD (998244353)
using namespace std; struct Edge{int to,next;}edge[];
LL T,n,x,ans,hash[],val[];
int head[],num_edge;
map<LL,LL>Map; void add(int u,int v)
{
edge[++num_edge].to=v;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Dfs(int x,int fa)
{
LL q[],tot=;
hash[x]=;
for (int i=head[x]; i; i=edge[i].next)
if (edge[i].to!=fa)
Dfs(edge[i].to,x),q[++tot]=hash[edge[i].to];
if (tot==){hash[x]=; return;}
sort(q+,q+tot+);
for (int i=; i<=tot; ++i)
hash[x]=(hash[x]+q[i]*val[i])%MOD;
} int main()
{
for (int i=; i<=; ++i)
val[i]=rand()*233473ll+rand()*19260817ll+rand();
scanf("%d",&T);
for (int t=; t<=T; ++t)
{
scanf("%d",&n);
memset(head,,sizeof(head)); num_edge=;
for (int i=; i<=n; ++i)
{
scanf("%d",&x);
if (!x) continue;
add(x,i), add(i,x);
}
ans=;
for (int i=; i<=n; ++i)
{
Dfs(i,-);
if (!Map[hash[i]]) Map[hash[i]]=t;
else Map[hash[i]]=min(Map[hash[i]],(LL)t);
ans=min(ans,Map[hash[i]]);
}
printf("%lld\n",ans);
}
}
BZOJ4337:[BJOI2015]树的同构(树hash)的更多相关文章
- 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 ...
- BZOJ 4337: BJOI2015 树的同构 树hash
4337: BJOI2015 树的同构 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4337 Description 树是一种很常见的数 ...
- BZOJ.4337.[BJOI2015]树的同构(树哈希)
BZOJ 洛谷 \(Description\) 给定\(n\)棵无根树.对每棵树,输出与它同构的树的最小编号. \(n及每棵树的点数\leq 50\). \(Solution\) 对于一棵无根树,它的 ...
- [BJOI2015]树的同构 && 树哈希教程
题目链接 有根树的哈希 离散数学中对树哈希的描述在这里.大家可以看看. 判断有根树是否同构,可以考虑将有根树编码.而编码过程中,要求保留树形态的特征,同时忽略子树顺序的不同.先来看一看这个方法: 不妨 ...
- BZOJ4337 树的同构 (树哈希)(未完成)
样例迷,没过 交了30pts #include <cstdio> #include <iostream> #include <cstring> #include & ...
- 【BZOJ4337】BJOI2015 树的同构 括号序列
[BZOJ4337]BJOI2015 树的同构 Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱 ...
- 刷题总结——树的同构(bzoj4337 树上hash)
Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...
- BZOJ4337:[BJOI2015]树的同构——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4337 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根, ...
随机推荐
- springmvc + spring + ibatis + mysql
1.spring mvc 官网下载:https://repo.spring.io/webapp/#/artifacts/browse/simple/General/libs-release-local ...
- springboot aop使用介绍
第一步:添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- C Primer Plus note4
warning: implicit declaration of function 'pirntf' [-Wimplicit-function-declaration]| 这种警告是因为使用了 没有声 ...
- js 密码 正则表达式
1. 代码 function checkPassword(str){ var reg1 = /[!@#$%^&*()_?<>{}]{1}/; var reg2 = /([a-zA- ...
- 139.00.003 Git学习-Git时光机之Inbox体系(三)
一.Git时光机之Inbox 体系 工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库. Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有G ...
- canvas.addEventListener()
对 canvas 元素的事件监听用addEventListener()实现, 但是有一点缺陷是:canvas 不支持键盘事件,为了解决这个问题,可以采用以下两种方案: 方案一: key event - ...
- idea maven打不了war包
开发的时候打不了war包,原因是 web.xml有问题或者是在idea里面webroot没有作为web引用, 添加之后WebRoot上面有个地球标志 就ok了
- javascript面向对象的写法03
javascript面向对象的写法03 js一些基础知识的说明 prototype 首先每个js函数(类)都有一个prototype的属性,函数是类.注意类有prototype,而普通对象没有. js ...
- 'weblogic.kernel.Default (self-tuning) 问题weblogic层面解决办法
声明:出现这个问题有程序方面.网络方面.weblogic设置方面等等原因,此文章主要讲述由于weblogic设置而导致的解决办法. 因为: 1.程序问题,需要项目自己去解决,weblogic在做优化处 ...
- 使用webBrowser下载文件
如果直接用webBrowser.Navigate("http://***.com/");会弹出文件下载的对话框. 而如果用webclient.UploadData()下载,对方网站 ...