题意:

给你一棵树,问你有多少个组合的相似;

相似是a结点的子树和b结点的子树的每一层的结点数相等;

思路:

HASH来搞;

主要也没学过散列表,以及一个散列函数的构造;

其实看下面程序很简单,手跑案例就可以发现,每个结点有:a*pri^b,系数a就是在该节点下的b层结点个数。

暂时只理解到这个层面上,以后能用这个思想再用吧;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
//LL powmod(LL a,LL b) {LL res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ULL pri;
map<ULL,LL>p;
VI g[100010];
ULL dfs(int x){
int m=SZ(g[x]);
ULL ans=1;
for(int i=0;i<m;i++){
ans+=dfs(g[x][i])*pri;
}
p[ans]++;
return ans;
}
int main()
{
pri=mod;
map<ULL,LL>::iterator it;
int n,u,v;
cin>>n;
for(int i=1;i<n;i++){
cin>>u>>v;
g[u].pb(v);
}
dfs(1);
LL ans=0;
for(it=p.begin();it!=p.end();it++){
LL a=(*it).se;
ans+=a*(a-1)/2;
}
cout<<ans<<endl;
return 0;
}

E. Similarity of Subtrees【hash】的更多相关文章

  1. 【hash】Similarity of Subtrees

    图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层 ...

  2. 【hash】BZOJ3751-[NOIP2014]解方程

    [题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少 ...

  3. 【hash】Power Strings

    [题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...

  4. 【hash】Seek the Name, Seek the Fame

    [哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over ...

  5. 【hash】A Horrible Poem

    [题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母 ...

  6. 【hash】Three friends

    [来源]:bzoj3916 [参考博客] BZOJ3916: [Baltic2014]friends [ 哈希和哈希表]Three Friends [Baltic2014][BZOJ3916]frie ...

  7. 湖南师范大学2018年大学生程序设计竞赛新生赛 A 齐神和心美的游戏【hash】

    [链接]:A [题意]:给你n个数的序列和k.判断是否可以三个数组成k(同一个数可以拿多次) [分析]:每个数vis记录一下.2层循环.两数之和不超过k以及剩下的数出现在序列中那么ok. [代码]: ...

  8. 【hash】珍珠

    [来源] https://loj.ac/problem/2427 [参考博客] LOJ#2427. 「POI2010」珍珠项链 Beads [题解]: 复杂度计算: 暴力枚举k每次计算是n/2+n/3 ...

  9. 洛谷P1117 优秀的拆分【Hash】【字符串】【二分】【好难不会】

    题目描述 如果一个字符串可以被拆分为AABBAABB的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串aabaabaaaabaabaa,如果令 A=aabA ...

随机推荐

  1. Ajax技术实现页面无刷新跳转

    Ajax实现无刷新显示新的页面 <!DOCTYPE html> <html> <head> <script src="/jquery/jquery- ...

  2. 解决Android Studio下Element layer-list must be declared问题

    近期将一个项目从Eclipse转到Android Studio. 项目中使用了环信demo中的一些xml资源,转换后发现color资源目录下诸如layer-list或者shape等标签报Element ...

  3. 2015年度新增开源软件排名TOP 100,EasyDarwin开源流媒体服务器排名第17

    本榜单包含 2015 年开源中国新收录的 5977 款开源软件中,根据软件本身的关注度.活跃程度进行排名前 100 名的软件.从这份榜单中或许可以了解到最新业界的趋势. 榜单详情:http://www ...

  4. Vue使用axios

    main.js-------------------   import axios from "axios"; import qs from "qs"; imp ...

  5. if __name__ == "__main__": 怎么理解?

    参考:https://www.cnblogs.com/Neeo/p/9504779.html 总结: 1.防止模块间调用时,执行被调用模块实例化执行,换句话说,就是不要执行调用模块原来实例化的内容 2 ...

  6. Memory usage of a Java process java Xms Xmx Xmn

    http://www.oracle.com/technetwork/java/javase/memleaks-137499.html 3.1 Meaning of OutOfMemoryError O ...

  7. FireMonkey下的WndProc实现

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Types, System.UI ...

  8. codeforces 466C. Number of Ways 解题报告

    题目链接:http://codeforces.com/problemset/problem/466/C 题目意思:给出一个 n 个数的序列你,问通过将序列分成三段,使得每段的和都相等的分法有多少种. ...

  9. linux应用之perl环境的安装(centos)

    1.安装Perl环境 yum install perl*这个命令基本上把perl的模块给安装齐了.yum install cpanCPAN这个就不用说了吧,大家都懂. 如果你对perl模块版本要求比较 ...

  10. 关于Spring Security的笔记

    1.web.xml配置文件 加载Spring Security,将DelegatingFilterProxy配置在DispatcherServlet之前. <filter> <fil ...