您的位置:首页 > 大数据 > 人工智能

Aizu - 2784Similarity of Subtrees(树哈希)

2017-08-26 20:14 190 查看
题意:

给你一棵树,问有多少对相似的子树(每层深度拥有相同数量的节点)。

思路:

将每棵树通过hash算法映射出一个值,下面这个公式用到了BKDRhash算法

Hash(T)= (n1+n2P+n3P2+…+nmPm−1)
% MOD /////////ni表示当前子树深度为i的节点有几个。

相当于将一棵树转化成一个p进制的整数。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
const ll p = 137;
const ll mod = 1e9+7;

int n;
ll Hash[maxn];
vector<int> arr[maxn];
map<ll,ll> mp;
map<ll,ll>::iterator it;

void dfs(int u)
{
Hash[u] = 1;
int len = arr[u].size();
for(int i = 0;i<len;i++)
{
int v = arr[u][i];
dfs(v);
Hash[u] = (Hash[u]+Hash[v]*p)%mod;
}
mp[Hash[u]]++;
}

int main()
{
scanf("%d",&n);
for(int i = 0;i<n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
arr[u].push_back(v);
}
dfs(1);
ll ans = 0;
for(it = mp.begin();it!=mp.end();it++)
{
ans += (it->second-1)*it->second/2;
}
printf("%lld\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: