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. poi 取excel单元格内容时,需要判断单元格的类型,才能正确取出

    以下内容非原创,原文链接http://blog.sina.com.cn/s/blog_4b5bc01101015iuq.html ate String getCellValue(HSSFCell ce ...

  2. ubuntu 安装包过程中遇到的一个错误解决办法

    错误提示如下: 将会安装下列额外的软件包: libdigest-hmac-perl libqt5test5下列[新]软件包将被安装: libdigest-hmac-perl下列软件包将被升级: lib ...

  3. 获取url参数,替换特殊字符

    function GetQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)( ...

  4. IOS-UISearchBar

    UISearchBar控件   最近用到搜索功能.于是,经过不断的研究,终于,有点懂了. 那就来总结一下吧,好记性不如烂笔头! 搜索,无疑可以使用UISearchBar控件! 那就先了解一下UISea ...

  5. es6环境中,export与import使用方法

    前言 参考自阮一峰大神的教程:http://es6.ruanyifeng.com/?search=export&x=6&y=5#docs/module#export-命令 声明:如有问 ...

  6. js生成中文二维码

    http://www.cnblogs.com/xcsn/archive/2013/08/14/3258035.html http://www.jb51.net/article/64928.htm 使用 ...

  7. CF910A

    题解: 简单dp dp[i]=min(dp[i-j])+1; 代码: #include<bits/stdc++.h> using namespace std; ; int n,m,dp[N ...

  8. springcloud- FeginClient 调用统一拦截添加请求头 RequestInterceptor ,被调用服务获取请求头

    使用场景: 在springcloud中通过Fegin调用远端RestApi的时候,经常需要传递一些参数信息到被调用服务中去,比如从A服务调用B服务的时候, 需要将当前用户信息传递到B调用的服务中去,我 ...

  9. KindEditor富文本编辑框和BeautifulSoup的基本使用

    KindEditor富文本编辑框 1.进入官网 2.下载 官网下载:http://kindeditor.net/down.php 本地下载:http://files.cnblogs.com/files ...

  10. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

    Given a string, find the length of the longest substring without repeating characters. For example, ...