Aizu 2784 Similarity of Subtrees
来源:互联网 发布:mysql 5.7.20安装教程 编辑:程序博客网 时间:2024/05/18 01:16
Problem
judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2784
vjudge.net/problem/Aizu-2784
Meaning
定义S(T,d):以 T 为根的子树中,深度为 d 的结点个数
(根的深度为 0)。
给定一棵树,问有多少对(i,j)满足:
- i < j
- 对任意非负的 d 都有:S(Ti,d)= S(Tj,d)
Analysis
可以将每一棵子树都 hash 成一个 P 进制的数,然后找 hash 值相同的那些子树更新答案。
设子树 T 有深都为 1、2、…、m 的后代结点,
Hash(T)= (
(相当于用 P 进制数,在位权为
Code
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 100000;const unsigned long long P = N + 3;int head[N+1], to[N], nxt[N];void add_edge(int f, int t, int sz){ to[sz] = t; nxt[sz] = head[f]; head[f] = sz;}// 用 unsigned long long 装 hash 值// 相当于 mod 2^63 - 1unsigned long long h[N+1]; // hashint son[N+1];void dfs(int now){ son[now] = h[now] = 0; for(int i = head[now]; ~i; i = nxt[i]) { ++son[now]; dfs(to[i]); h[now] += h[to[i]] * P; } h[now] += son[now];}int main(){ int n; scanf("%d", &n); memset(head, -1, sizeof head); for(int i = 1, f, t, sz = 0; i < n; ++i) { scanf("%d%d", &f, &t); add_edge(f, t, sz++); } dfs(1); sort(h + 1, h + n + 1); long long ans = 0; for(int i = 1, j; i <= n; i = j) { for(j = i; j <= n && h[j] == h[i]; ) ++j; ans += (long long)(j - i) * (j - i - 1) >> 1; } printf("%lld\n", ans); return 0;}
阅读全文
0 0
- Aizu 2784 Similarity of Subtrees
- Similarity of Subtrees 深搜,hash
- E. Similarity of Subtrees【hash】
- BNUOJ 52310 Similarity of Subtrees
- Problem E Similarity of Subtrees
- JAG Practice2016-E Similarity of Subtrees- hash
- Similarity of Subtrees(深搜+hash、映射)
- 2016 JAG E Similarity of Subtrees(hash)
- (2016 弱校联盟十一专场10.3) Similarity of Subtrees DFS + hash
- 2016 ACM-ICPC Asia Regional E – Similarity of Subtrees(dfs+hash)
- Calculation of Vector Similarity
- Measure of similarity
- Similarity of Social Media Network
- Aizu
- Aizu
- Aizu
- Aizu
- Aizu
- 在线笔试的诀窍
- 日夜间模式
- Python环境安装(3.5.4)
- Parenthesis(括号平衡串,匹配问题)
- LeetCode 78. Subsets (Python)
- Aizu 2784 Similarity of Subtrees
- oi中犯过的2b错误
- 实现日夜间转换获取数据上下刷新(main)
- MAC版Eclipse的常用快捷键
- 使用Excel2013制作甘特图
- 8.13
- [BZOJ3389]信号连接
- shell中根据日期进行循环
- Kotlin 包管理