vjudge

Description

给一棵\(n\)个节点的有根树,定义两棵树同构当且仅当他们每个深度的节点个数相同。问这个树上有多少对子树满足同构。\(n\le100000\)。

sol

树\(hash\)。

每个深度的节点个数,类似于一个多项式?所以定义\(hash\)函数:

\[Hash(u)=base1+base2\sum Hash(v)
\]

最后判相等就不说了,\(sort\)一遍再搞一搞就好了。

code

在我交换\(base1\)和\(base2\)的值之前它是\(WA\)的。。。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int gi(){
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
#define ull unsigned long long
const int N = 2e5+5;
const ull base2 = 20020415;
const ull base1 = 20011118;
int n,to[N],nxt[N],head[N],cnt;
ull Hash[N],ans;
void link(int u,int v){
to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;
}
void dfs(int u,int f){
Hash[u]=base1;
for (int e=head[u];e;e=nxt[e])
if (to[e]!=f) dfs(to[e],u),Hash[u]+=Hash[to[e]]*base2;
}
int main(){
n=gi();
for (int i=1;i<n;++i){
int u=gi(),v=gi();
link(u,v);link(v,u);
}
dfs(1,0);sort(Hash+1,Hash+n+1);
for (int i=1,j;i<=n;i=j){
j=i+1;
while (j<=n&&Hash[j]==Hash[i]) ++j;
ans+=1ll*(j-i)*(j-i-1)/2;
}
printf("%llu\n",ans);return 0;
}

[Aizu2784]Similarity of Subtrees的更多相关文章

  1. Aizu 2784 Similarity of Subtrees(树哈希)

    Similarity of Subtrees Define the depth of a node in a rooted tree by applying the following rules r ...

  2. E. Similarity of Subtrees【hash】

    题意: 给你一棵树,问你有多少个组合的相似: 相似是a结点的子树和b结点的子树的每一层的结点数相等: 思路: HASH来搞: 主要也没学过散列表,以及一个散列函数的构造: 其实看下面程序很简单,手跑案 ...

  3. 【hash】Similarity of Subtrees

    图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层 ...

  4. 字符串Hash/树Hash学习笔记

    哈希 Tags:字符串 作业部落 评论地址 一.概述 百度百科: 散列表(Hash table/哈希表),是根据关键码值(Key value)而直接进行访问的数据结构. 哈希表常用于比较两个字符串是否 ...

  5. HASH算法小结

    一.简述 HASH算法的本质是特征提取——将某种不太好表示的特征,通过某种压缩的方式映射成一个值.这样,就可以优雅解决一部分难以解决的特征统计问题. 同时考虑到hash算法的本质是个概率算法,因此并不 ...

  6. JAG Practice Contest for ACM-ICPC Asia Regional 2016

    2016弱校联盟十一专场10.3 传送门 B. Help the Princess! 计算皇后和士兵谁先到达出口即可. C. We don't wanna work! 两个优先队列维护工作中积极性最小 ...

  7. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  8. 机器学习中的相似性度量(Similarity Measurement)

    机器学习中的相似性度量(Similarity Measurement) 在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间 ...

  9. cosine similarity

    Cosine similarity is a measure of similarity between two non zero vectors of an inner product space  ...

随机推荐

  1. spark streaming之 windowDuration、slideDuration、batchDuration​

    spark streaming 不同于sotm,是一种准实时处理系统.storm 中,把批处理看错是时间教程的实时处理.而在spark streaming中,则反过来,把实时处理看作为时间极小的批处理 ...

  2. javascript模块化编写

    目录: 1. 开篇语 2. 对象形式写法 3. 立即执行函数写法 4. prototype写法 5. 总结 1. 开篇语 现在我们写代码不再是一个人包办所有的活儿,都是在多人合作的情况下完成的.我们只 ...

  3. mysql数据库优化课程---18、mysql服务器优化

    mysql数据库优化课程---18.mysql服务器优化 一.总结 一句话总结: 1.四种字符集问题:字符集都设置为utf-82.slow log慢查询日志问题3.root密码丢失 1.mysql存在 ...

  4. golang简易版聊天室

    功能需求: 创建一个聊天室,实现群聊和单聊的功能,直接输入为群聊,@某人后输入为单聊 效果图: 群聊:   单聊: 服务端: package main import ( "fmt" ...

  5. HDU 3923 Invoker(polya定理+乘法逆元(扩展欧几里德+费马小定理))

    Invoker Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 122768/62768K (Java/Other) Total Subm ...

  6. Composer 安装东西遇到github需要token怎么办

    安装yii2遇到这样的提示: Could not fetch https://api.github.com/repos/jquery/sizzle/contents/bower.json?ref=91 ...

  7. 求小于等于k长度的最大区间和

    题意 给出一个序列,求长度小于等于k的最大区间和并输出起点和终点 1<=n<=100000 1<=k<=n   题解:先算出前缀和,利用单调队列的性质,在单调队列中存储sum[ ...

  8. The main points of capacitive screen technology

  9. jquery表单验证插件 jquery.form.js-转

    来自:http://www.cnblogs.com/luluping/archive/2009/04/15/1436177.html Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满 ...

  10. C# 如何判断字符串中是否包含另一个字符串?

    如  字符串1(str1)为:“你好怎么解决呢!”    字符串2(str2)为:“你好” 如果str1里面包str2 则 Response.Write("成功");否则 Resp ...