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条边的连通无向图称为树. 若将某个点作为根, ...
随机推荐
- OOP_由C到C++
由C到C++ OOP第一课 C语言的局限 C++的特点 C++的程序特征 C++程序的结构特性 C++程序的编辑.编译和运行 ⭐C++对C的补充 C语言的局限 类型检查机制相对较弱,使得程序中的一些错 ...
- disruptor--Introduction
The best way to understand what the Disruptor is, is to compare it to something well understood and ...
- css样式美化 下拉框 select 样式
<span class="setleft wid80"><span class="fyhbx">*</span>入库类型 : ...
- 使用 Python 管理 Azure:基础配置
Azure 提供了丰富的 Python SDK 来对 Azure 进行开发管理,包括使用 Azure 的开源框架在 Azure 上创建 web 应用程序,对 Azure 的虚拟机,存储等进行管理,本系 ...
- Vue指令(四)--v-model
1.v-model的使用场景 1.v-model的使用,用于表单控件的数据绑定 2.v-model与value共同使用,实现选项框的选中事件,两者相同时,选中 3.v-model 与v-bind:va ...
- JavaMail 邮件发送
jar包部署 /** * 通过SMTP进行邮件集成 */ public class CmpSendMail { // 邮件发送服务器主机 private final static String HOS ...
- IDEA集成 SpringBoot+Mybaties 之 @Autowired注入报错
原因分析: 因为@Mapper注解是由ibates提供的,需要在application.yml里加上下图配置 以及在启动类入口加上 扫描你mapper接口所在的包 ,所以Spring容器是不认识这个注 ...
- Tomcat源码(二):tomcat启动之前的初始化
当tomcat启动的时候 首先会加载 org.apache.ctalina.startup.BootStrap类. 使用eclipse或idea启动tomcat其实就是在启动这个类的main方法 根据 ...
- JBPM学习第3篇:10分钟熟悉JBPM工作台
1.打开:http://localhost:8080/jbpm-console 键入用户名和密码(krisv/krisv)登陆. 2.看视频: http://download.jboss.org/jb ...
- csharp: using OleDb Getting the identity of the most recently added record
/// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString&quo ...