JAG Practice2016-E Similarity of Subtrees- hash
来源:互联网 发布:淘宝店铺产品布局 编辑:程序博客网 时间:2024/05/21 14:49
https://acm.bnu.edu.cn/v3/statments/jag2016.pdf
题意:
num(T1,d) 为树T1在深度为d这一层的子节点数量
定义 两棵树相似 的条件为
对所有的d, S(T,d) equals S(T ′ ,d)
从叶子节点开始bfs,每个节点的相似度信息可以hash成一个数。
从当前节点开始去更新父亲节点的相似度信息
只有当一个节点的所有儿子都访问完了(也即得到了自身完整的相似度信息),才进入队列
在每一层计算一次,具有相同相似度的节点对答案的贡献为 C(X,2)
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <queue>#include <map>#include <set>#include <vector>using namespace std;const int N=100005;typedef long long ll;vector<int >mp[N];int son_num[N];int son_come[N];unsigned long long hash_val[N];int fa[N];void dfs(int x,int f){ for (int i=0; i<mp[x].size(); i++) { int v=mp[x][i]; if (v==f)continue; fa[v]=x; son_num[x]++; dfs(v,x); }}map<unsigned long long ,int> vis;map<unsigned long long ,int> ::iterator it;struct node{ int dep,x; node() {} node(int a,int b) { x=a,dep=b; }};queue<node> q;int main(){ int n,u,v; cin>>n; for (int i=1; i<n; i++) { scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u); } dfs(1,0); for (int i=1; i<=n; i++) if (!son_num[i]) q.push(node(i,1)); ll ans=0; while(!q.empty()) { int sz=q.size(); vis.clear(); for (int i=1; i<=sz; i++) { node tp=q.front(); q.pop(); hash_val[tp.x]+=239; vis[hash_val[tp.x]]++; int f=fa[tp.x]; hash_val[f]+=hash_val[tp.x]*239; son_come[f]++; if (son_come[f]==son_num[f]) q.push(node(f,tp.dep+1)); } for (it=vis.begin();it!=vis.end();it++) ans+= 1LL*it->second*(it->second-1)/2; } printf("%lld\n",ans); return 0;}
0 0
- JAG Practice2016-E Similarity of Subtrees- hash
- 2016 JAG E Similarity of Subtrees(hash)
- E. Similarity of Subtrees【hash】
- Similarity of Subtrees 深搜,hash
- Problem E Similarity of Subtrees
- Similarity of Subtrees(深搜+hash、映射)
- 2016 ACM-ICPC Asia Regional E – Similarity of Subtrees(dfs+hash)
- BNUOJ 52310 Similarity of Subtrees
- Aizu 2784 Similarity of Subtrees
- (2016 弱校联盟十一专场10.3) Similarity of Subtrees DFS + hash
- JAG Practice2016-C-We don't wanna work!-模拟
- Calculation of Vector Similarity
- Measure of similarity
- CodeForces 674E Bear and Destroying Subtrees
- Similarity of Social Media Network
- CodeForces 643E Bear and Destroying Subtrees (概率)
- The Statistics of Sequence Similarity Scores
- POJ 2754 Similarity of necklaces 2
- at sun.reflect.NativeMethodAccessorImpl.invoke0
- Markdown|书单
- 用键盘控制改变实体色彩
- thinkpad t430u win10小红点无反应/停止响应
- 【NOIP提高组五校联考】道路规划
- JAG Practice2016-E Similarity of Subtrees- hash
- Mybatis异常 Parameter "..." not found
- 第一次出现一次的字符
- (转)如何学习盈透api的开发?
- Java并发:其他(总结性的东西)
- hdu5524Subtrees+递归模拟
- Unity3D 更新远程文件下载器
- javaweb mvc设计模式
- 51 单片机晶振电路原理