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. centOS 安装Python3与python2并存

    如果本机安装了`python2`,尽量不要管他,使用`python3`运行python脚本就好,因为可能有程序依赖目前的`python2`环境, 比如`yum`!!!!! 不要动现有的`python2 ...

  2. 【转】 GRASP(通用职责分配软件模式)模式

    转自:http://www.cnblogs.com/sevenyuan/archive/2010/03/05/1678730.html 及:http://blog.csdn.net/lovelion ...

  3. 要back的题目 先立一个flag

    要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...

  4. ios资源加载策略

    做了好几个月的ios,大框架都是别人搭好的,自己只是实现逻辑,很是失落.慢慢开始整理学习一些概念类的东西吧,希望自己能提高点. cocos2d-x从cocos2d-2.0-x-2.0.2开始,考虑到自 ...

  5. MVC4 AspNet MVC下的Ajax / 使用JQuery做相关的Ajax请求

    源码参考:链接:http://pan.baidu.com/s/1pKhHHMj  密码:mkr4 1:新建-->项目-->Web-->ASP.NET MVC 4 Web 应用程序.命 ...

  6. aspnet_regiis.exe -i 执行报错

    IIS刚部署时出现问题 处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” 按照网上的步骤,使用管理员打开CMD 开始->所有程 ...

  7. Python 模块搜索路径 -- (转)

    最近在看<Python源码剖析>,对Python内部运行机制比以前了解的更深入了,感觉自己有机会也可以做个小型的动态脚本语言了,呵呵,当然是吹牛了.目的当然不是创造一个动态语言,目的只有一 ...

  8. Override 和 Overload 的含义和区别

    Override 1.方法重写.覆盖: 2.重写是父类与子类之间多态性的一种表现: 3.方法名,参数,返回值相同: 4.存在于子类和父类之间: 5.修饰为final的方法,不能被重写: Overloa ...

  9. Linux信号函数

    1. signal函数: #include <signal.h> void (*signal(int signo, void (*func)(int)))(int); ret-成功返回信号 ...

  10. 64_g2

    gettext-libs-0.19.8.1-9.fc26.x86_64.rpm 15-Mar-2017 14:15 305038 gf2x-1.1-9.fc26.i686.rpm 11-Feb-201 ...