Similarity of Subtrees

Define the depth of a node in a rooted tree by applying the following rules recursively:

  • The depth of a root node is 0.
  • The depths of child nodes whose parents are with depth d

are d+1

  • .

Let S(T,d)

be the number of nodes of T with depth d. Two rooted trees T and T′ are similar if and only if S(T,d) equals S(T′,d) for all non-negative integer d

.

You are given a rooted tree T

with N nodes. The nodes of T are numbered from 1 to N. Node 1 is the root node of T. Let Ti be the rooted subtree of T whose root is node i. Your task is to write a program which calculates the number of pairs (i,j) such that Ti and Tj are similar and i<j

.

Input

The input consists of a single test case.

N

a1 b1
a2 b2
...
aN−1 bN−1

The first line contains an integer N

(1≤N≤100,000), which is the number of nodes in a tree. The following N−1 lines give information of branches: the i-th line of them contains ai and bi, which indicates that a node ai is a parent of a node bi. (1≤ai,bi≤N,ai≠bi

)
The root node is numbered by 1. It is guaranteed that a given graph is a
rooted tree, i.e. there is exactly one parent for each node except the
node 1, and the graph is connected.

Output

Print the number of the pairs (x,y)

of the nodes such that the subtree with the root x and the subtree with the root y are similar and x<y

.

Sample Input 1

5
1 2
1 3
1 4
1 5

Output for the Sample Input 1

6

Sample Input 2

6
1 2
2 3
3 4
1 5
5 6

Output for the Sample Input 2

2

Sample Input 3

13
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
6 10
7 11
8 12
11 13

Output for the Sample Input 3

14
【分析】现在定义两棵树相似,仅当两棵树任意层次的节点数相等。然后给你一棵树,问你有多少棵子树是相似的。
类似字符串,我们可以将子树哈希,然后直接比较哈希值就可以了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N = 1e5+;
const ll p = ;
const ll mod = 1e9+;
const double eps = 1e-;
int n,m,k;
ll has[N];
vector<int>edg[N];
map<ll,ll>mp;
map<ll,ll>::iterator it;
void dfs(int u,int fa){
has[u]=;
for(int v:edg[u]){
dfs(v,u);
has[u]=(has[u]+has[v]*p)%mod;
}
mp[has[u]]++;
}
int main()
{
int u,v;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
edg[u].push_back(v);
}
dfs(,);
ll ans=;
for(it =mp.begin();it!=mp.end();it++){
ans+=(it->second-)*it->second/;
}
printf("%lld\n",ans);
return ;
}

Aizu 2784 Similarity of Subtrees(树哈希)的更多相关文章

  1. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

  2. UOJ#373. 【ZJOI2018】线图 搜索,树哈希,动态规划

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ373.html 前言 真是一道毒瘤题.UOJ卡常毒瘤++.我卡了1.5h的常数才过QAQ Orzjry 标算居然是指数做法 ...

  3. LOJ.6066.[2017山东一轮集训Day3]第二题(树哈希 二分)

    LOJ 被一件不愉快的小事浪费了一个小时= =. 表示自己(OI方面的)智商没救了=-= 比较显然 二分+树哈希.考虑对树的括号序列进行哈希. 那么每个点的\(k\)子树的括号序列,就是一段区间去掉距 ...

  4. 【BZOJ5211】[ZJOI2018]线图(树哈希,动态规划)

    [BZOJ5211][ZJOI2018]线图(树哈希,动态规划) 题面 BZOJ 洛谷 题解 吉老师的题目是真的神仙啊. 去年去现场这题似乎骗了\(20\)分就滚粗了? 首先\(k=2\)直接算\(k ...

  5. BZOJ.4337.[BJOI2015]树的同构(树哈希)

    BZOJ 洛谷 \(Description\) 给定\(n\)棵无根树.对每棵树,输出与它同构的树的最小编号. \(n及每棵树的点数\leq 50\). \(Solution\) 对于一棵无根树,它的 ...

  6. bzoj4337: BJOI2015 树的同构 树哈希判同构

    题目链接 bzoj4337: BJOI2015 树的同构 题解 树哈希的一种方法 对于每各节点的哈希值为hash[x] = hash[sonk[x]] * p[k]; p为素数表 代码 #includ ...

  7. 【BZOJ3162】独钓寒江雪(树哈希,动态规划)

    [BZOJ3162]独钓寒江雪(树哈希,动态规划) 题面 BZOJ 题解 忽然翻到这道题目,突然发现就是前几天一道考试题目... 题解: 树哈希,既然只考虑这一棵树,那么,如果两个点为根是同构的, 他 ...

  8. 线段树+哈希【CF580E】Kefa and Watch

    线段树+哈希[CF580E]Kefa and Watch Description \(n\)个数的字符串,\(m + k\)个操作 1 l r k把\(l - r\)赋值为\(k\) 2 l r d询 ...

  9. P4323 [JSOI2016]独特的树叶(树哈希)

    传送门 树哈希?->这里 反正大概就是乱搞--的吧-- //minamoto #include<bits/stdc++.h> #define R register #define l ...

随机推荐

  1. 数据结构&字符串:可持久化字典树

    利用可持久化Trie树实现范围内取值异或最大值 如果标题没有表达清楚意思,可以看这里的题干: 然后根据异或的性质,异或一个数两次相当于没有异或,那么我们可以维护一个异或前缀和 有了异或前缀和之后我们就 ...

  2. 【C++对象模型】第六章 执行期语意学

    执行期语意学,即在程序执行时,编译器产生额外的指令调用,确保对象的构造,内存的释放,以及类型转换与临时对象的生成的安全进行. 1.对象的构造和析构 对于类对象的构造,一般在定义之后则开始内部的构造过程 ...

  3. redis启动脚本

    #!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the ...

  4. Part1-HttpClient快速入门案例

    前言 最近这段时间在学习爬虫方面的知识,接触了几个优秀的爬虫框架,也爬取了一些自己喜欢网站的信息.通过官网学习HttpClient的过程中,希望通过写此博客来巩固自己的学习,也为有需要的人提供学习帮助 ...

  5. hydra 密码破解工具详解

    一.简介 hydra是著名黑客组织thc的一款开源的暴力密码破解工具,可以在线破解多种密码.官 网:http://www.thc.org/thc-hydra,可支持AFP, Cisco AAA, Ci ...

  6. Linux从入门到放弃

    Ch.0 几点Linux常识 Linux严格区分大小写,不像windows中命令是不区分大小写的 Linux中所有内容以文件形式保存,包括硬件 Linux不靠扩展名区分文件类型,所有扩展名只是为了方便 ...

  7. MACACA===gradle下载和安装

    gradle下载地址: http://services.gradle.org/distributions/ 或者直接点击这个: http://services.gradle.org/distribut ...

  8. 2015多校第7场 HDU 5378 Leader in Tree Land 概率DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:一棵n个节点的树.对其节点进行标号(1~n).求恰好存在k个节点的标号是其节点所在子树的最 ...

  9. 502 bad gateway,ngix

    1.ftm对nginx的解析出现问题:20171228

  10. leetcode 之Partition List(16)

    思路就是定义两个链表,一个放大的,一个放小的,最后将两个连起来,注意细节问题. ListNode *partionList(ListNode *head, int value) { ListNode ...