[Aizu2784]Similarity of Subtrees
Description
给一棵\(n\)个节点的有根树,定义两棵树同构当且仅当他们每个深度的节点个数相同。问这个树上有多少对子树满足同构。\(n\le100000\)。
sol
树\(hash\)。
每个深度的节点个数,类似于一个多项式?所以定义\(hash\)函数:
\]
最后判相等就不说了,\(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的更多相关文章
- Aizu 2784 Similarity of Subtrees(树哈希)
Similarity of Subtrees Define the depth of a node in a rooted tree by applying the following rules r ...
- E. Similarity of Subtrees【hash】
题意: 给你一棵树,问你有多少个组合的相似: 相似是a结点的子树和b结点的子树的每一层的结点数相等: 思路: HASH来搞: 主要也没学过散列表,以及一个散列函数的构造: 其实看下面程序很简单,手跑案 ...
- 【hash】Similarity of Subtrees
图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层 ...
- 字符串Hash/树Hash学习笔记
哈希 Tags:字符串 作业部落 评论地址 一.概述 百度百科: 散列表(Hash table/哈希表),是根据关键码值(Key value)而直接进行访问的数据结构. 哈希表常用于比较两个字符串是否 ...
- HASH算法小结
一.简述 HASH算法的本质是特征提取——将某种不太好表示的特征,通过某种压缩的方式映射成一个值.这样,就可以优雅解决一部分难以解决的特征统计问题. 同时考虑到hash算法的本质是个概率算法,因此并不 ...
- JAG Practice Contest for ACM-ICPC Asia Regional 2016
2016弱校联盟十一专场10.3 传送门 B. Help the Princess! 计算皇后和士兵谁先到达出口即可. C. We don't wanna work! 两个优先队列维护工作中积极性最小 ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- 机器学习中的相似性度量(Similarity Measurement)
机器学习中的相似性度量(Similarity Measurement) 在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间 ...
- cosine similarity
Cosine similarity is a measure of similarity between two non zero vectors of an inner product space ...
随机推荐
- IDEA 中使用 Vue 提示 namespace is not bound
今天在 IDEA 中使用 vue.js 时提示我如下错误信息 解决方法: 把这个校验项目去掉就可以了.
- opencv颜色提取color filting
# -*- coding: utf-8 -* import numpy as np import cv2 cap = cv2.VideoCapture(0) while True: _ , frame ...
- js获取显示器、页面等高度 (转)
网页可见区域宽:document.body.clientWidth网页可见区域高:document.body.clientHeight网页可见区域宽:document.body.offsetWidth ...
- ios 不兼容 报错NaN
function GetDateDiff(DiffTime) { //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式 Time = DiffTime.replace(/\-/g, ...
- ZOJ 2971 Give Me the Number (模拟,字符数组的清空+map)
Give Me the Number Time Limit: 2 Seconds Memory Limit: 65536 KB Numbers in English are written ...
- python金融分析项目
1.进入ipython: C:\Users\Administrator>ipython Python (v3. , ::) [MSC v. bit (AM D64)] Type 'copyrig ...
- DRF中两大视图基类APIView/GenericAPIView的介绍
(1)APIView rest_framework.views.APIView APIView是REST framework提供的所有视图的基类,继承自Django的View父类. APIView与V ...
- html hidefocus="true"
最近学到html,看到别人写的代码带hidefocus="true",查了一下是使超链接不显示周围的虚线. hideFocus即隐藏聚焦,具有使对象聚焦失效的功能,其功能相当于: ...
- c# 文件日志处理 需要log4net配置
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- 使用pipework将Docker容器配置到本地网络环境中
使用pipework将Docker容器配置到本地网络环境中 需求 在使用Docker的过程中,有时候我们会有将Docker容器配置到和主机同一网段的需求.要实现这个需求,我们只要将Docker容器和主 ...