题目链接

https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310

problem description

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 … 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. 14

Sample Input1

5

1 2

1 3

1 4

1 5

Output for the Sample Input 1

6

Sample Input2

6

1 2

2 3

3 4

1 5

5 6

Output for the Sample Input 2

2

Sample Input3

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

题意:输入一棵由n个点和n-1条边构成的树,求这个树中两棵相似的子树有多少对? 相似的子树:要求在相同的深度,两颗子树的在这一层的节点数相同;

思路:深搜,hash表示每一个点为子树时的子树状态;

第三组样例:对图上每个点,给一个标识p^d d为这个点的深度,那么每个点的子树状态就可以用子树上所有项的和表示,如2号节点p^4+2p^3+2p^2+p   3号节点p^4+2p^3+2p^2+p 它们的多项式相同,为了方便用map映射统计,可以给p赋一个值,为了减小冲突可以取一个较大的质数,这就是hash;

代码如下:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const LL maxn=1e6+,p=,mod=1e9+;
vector<LL>G[maxn];
LL hash_v[maxn];
map<LL,LL>mp;
map<LL,LL>::iterator it; void dfs(LL u)
{
hash_v[u]=;
for(LL i=; i<G[u].size(); i++)
{
LL v=G[u][i];
dfs(v);
hash_v[u]=(hash_v[u]+hash_v[v]*p)%mod;
}
mp[hash_v[u]]++;
}
int main()
{
///freopen("in.txt","r",stdin);
LL n;
LL u,v;
while(scanf("%lld",&n)!=-)
{
for(LL i=; i<=n; i++)
G[i].clear();
mp.clear();
for(LL i=; i<n; i++)
{
scanf("%lld%lld",&u,&v);
G[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 ;
}

2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)的更多相关文章

  1. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  2. 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8506#problem/A problem description As we know, t ...

  3. (2016弱校联盟十一专场10.3) D Parentheses

    题目链接 把左括号看成A右括号看成B,推一下就行了.好久之前写的,推到最后发现是一个有规律的序列. #include <bits/stdc++.h> using namespace std ...

  4. (2016弱校联盟十一专场10.3) B.Help the Princess!

    题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...

  5. (2016弱校联盟十一专场10.3) A.Best Matched Pair

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...

  6. 2016弱校联盟十一专场10.3---We don't wanna work!(STL--set的使用)

    题目链接 https://acm.bnu.edu.cn/v3/contest_show.php?cid=8504#problem/C 代码如下: #include <iostream> # ...

  7. (2016弱校联盟十一专场10.2) A.Nearest Neighbor Search

    题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...

  8. (2016弱校联盟十一专场10.2) E.Coins

    题目链接 很久之前写的了,好像是对拍打表过的,推一下就行了. #include <bits/stdc++.h> using namespace std; typedef long long ...

  9. (2016弱校联盟十一专场10.5) F. Fibonacci of Fibonacci

    题目链接 题目大意就是这个,先找出下标的循环节,再快速幂对20160519取余就行了. 找出下标循环节: #include <cstdio> #include <iostream&g ...

随机推荐

  1. 大叔也说Xamarin~Android篇~日志的记录

    回到目录 无论哪个平台,开始哪种应用程序,日志总是少不了的,大家在Lind.DDD里也可以看到大叔的日志组件,而在xamarin进行移动开发时,为了更好的调试,记录运行的情况,日志也是必须的,这讲主要 ...

  2. php实现注册

    <?php header("Content-Type:text/html;charset=gb2312"); @mysql_connect('localhost','root ...

  3. fir.im Weekly - 这是一份强大的 SwiftGuide

    大新闻!Apple 10 亿美元融资滴滴!库克大叔对中国 iOS 开发者表达了高度认可,同时也传出 iOS 10 将内置滴滴 App 的消息.想像下,某个加班的深夜飙完代码,最性感的事情莫过于:「Si ...

  4. gulp学习笔记4

    gulp系列学习笔记: 1.gulp学习笔记1 2.gulp学习笔记2 3.gulp学习笔记3 4.gulp学习笔记4 之前的任务都是单个的,比较简单.接下去我们开始引用多个插件,一次性把任务搞定,省 ...

  5. 在android中用跑马灯的效果显示textview

    大家好,在我们通常的android project中,通常需要用到textview这一个布局文件,并且对于这一个显示布局所需要的文本文字内容. 下面我们就来介绍一种方法来实现在android中用跑马灯 ...

  6. base64:URL背景图片与web页面性能优化

    一.base64百科 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,可用于在HTTP环境下传递较长的标识信息. 某人: 唉,我彻底废柴了,为何上面明明是中文,洒家却看不懂嘞,为什 ...

  7. Parametric Curves and Surfaces

    Parametric Curves and Surfaces eryar@163.com Abstract. This paper is concerned with parametric curve ...

  8. 文本溢出text-overflow和文本阴影text-shadow

    前面的话 CSS3新增了一些关于文本的样式,其中text-overflow文本溢出和text-shadow文本阴影有些特别.因为它们有对应的overflow溢出属性和box-shadow盒子阴影属性. ...

  9. poj 2031Building a Space Station(几何判断+Kruskal最小生成树)

    /* 最小生成树 + 几何判断 Kruskal 球心之间的距离 - 两个球的半径 < 0 则说明是覆盖的!此时的距离按照0计算 */ #include<iostream> #incl ...

  10. Java 集合系列目录(Category)

    下面是最近总结的Java集合(JDK1.6.0_45)相关文章的目录. 01. Java 集合系列01之 总体框架 02. Java 集合系列02之 Collection架构 03. Java 集合系 ...