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条边的连通无向图称为树. 若将某个点作为根, ...
随机推荐
- Web后台模拟前端post(带NTLM验证)
using System.Data; using System.Net; using System.IO; using System.Net.Http; using System.Web; using ...
- Asp.net core如何使用Session
转自:https://tahirnaushad.com/2017/08/18/asp-net-core-session-state/ Asp.net core使用session: 在nuget 安装M ...
- K2.ActivityInstanceDestination.User is NULL
his one caught me by surprise! I needed to build up a CSV list of email addresses for all previous t ...
- php array 数组及数组索引
array (PHP 4, PHP 5, PHP 7) array — 新建一个数组 说明 array array ([ mixed $... ] ) 创建一个数组.关于数组是什么的信息请阅读数组一节 ...
- MySQL prompt命令
修改提示符,设置后挺方便的 例如: 几个好用的参数 \d 当前数据库 \u 当前用户 \h 当前主机 更多参数可以参考mysol官方文档 参考文档:https://dev.mysql.com/doc/ ...
- C语言转置矩阵算法
对一个nxn阶的矩阵进行转置,算法如下: #include <stdio.h> #define n 3 void MM(int a[][n]) { int i,j,temp; ;i < ...
- 解决PUTTY出现中文乱码问题
Putty在默认情况下没有使用UTF-8编码,因此在显示中文的时候会出现乱码.解决方法非常简单:按照下图,在连接之前,左边窗口中的“转换”选项卡(如图所示),然后在右边“假定接收的数据字符集”的下拉选 ...
- css层叠性和继承性
1.了解css层叠性 层叠性是什么?就是解决处理css选择器和属性冲突的能力.css的选择器权重是分大小,就是当多个选择器都选中了同一个标签时,听谁的??? 标签选择器 < 类选择器 < ...
- Scrum团队开发
Scrum学习心得 什么是scrum Scrum是迭代式增量软件开发过程,通常用于敏捷软件开发.Scrum包括了一系列实践和预定义角色的过程骨架.Scrum中的主要角色包括同项目经理类似的Scrum主 ...
- C语言数组指针(指向数组的指针)
注意:数组指针的定义,与指针数组的区别 转载:http://c.biancheng.net/cpp/biancheng/view/162.html 指向多维数组元素的指针变量 ① 指向数组元素的指针变 ...