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 ;
}

最新文章

  1. CSS3动画(个人理解)
  2. 细化如何安装LNMP + Zabbix 监控安装文档以及故障排除
  3. Leetcode-203 Remove Linked List Elements
  4. 那万恶的ssh真是麻烦
  5. CSS modules 与 React中实践
  6. 在oracle中怎么把一张表的数据插入到另一张表中
  7. Hadoop-Yarn-框架原理及运作机制(原理篇)
  8. python实现了字符串的按位异或和php中的strpad函数
  9. 从头开始学JavaScript (六)——语句
  10. VS2013 图片背景&#183;全透明背景图(转)
  11. ORACLE数据库学习之备份与恢复
  12. D3中的each() 以及svg defs元素 clipPath的使用
  13. java -jar和hadoop jar的区别
  14. LeetCode724. 寻找数组的中心索引
  15. elixir mix 简介
  16. Linux命令第一篇
  17. PAT 乙级 1091 N-自守数 (15 分)
  18. Node.js的环境搭建
  19. 加快Android Studio的编译速度
  20. http和WebSocket

热门文章

  1. LightOJ 1023 Discovering Permutations 水题
  2. MSSQL Get Last Monday and Last Sunday
  3. 快速排序Quick sort
  4. YII 框架查询
  5. PAT L2-017. 人以群分
  6. Python的异常处理机制 -- (转)
  7. linux 服务简介
  8. linux中字符串转换函数 simple_strtoul【转】
  9. Linux 入门记录:二、Linux 文件系统基本结构
  10. .net爬虫了解一下