zoj 3602 Count the Trees(二叉树性质)
来源:互联网 发布:广东进出口数据 编辑:程序博客网 时间:2024/05/16 11:24
题意:
给出两个二叉树A, B。找满足以下条件的pair的个数。
pair(T, F), T, F分别为A,B子树,且T=F。
思路:
不错的题目。。加强对二叉树的理解。。
两棵二叉树相同的充要条件是左右子树相同。
我们给每个形态一个编号。A的编号为id(A)
则每棵树可以表示为 (id(left_child), id(right_child))
然后统计一下就可以了。
struct node { int l, r;};bool operator < (const node &lhs, const node &rhs) { if (lhs.l != rhs.l) return lhs.l < rhs.l; return lhs.r < rhs.r;}int n, m, countor;node ai[Maxn+5], bi[Maxn+5];map<node, int> Hash;map<int, int> cnt_a, cnt_b;int get_hash(int l, int r) { node tmp = (node) {l, r}; if (Hash.count(tmp)) return Hash[tmp]; return Hash[tmp] = countor ++;}int gao(int x, node *arr, map<int, int> &cnt) { int l, r; if (arr[x].l > 0) { l = gao(arr[x].l, arr, cnt); } else l = -1; if (arr[x].r > 0) { r = gao(arr[x].r, arr, cnt); } else r = -1; int h = get_hash(l, r); cnt[h] += 1; return h;}int main() {#ifndef ONLINE_JUDGE freopen("input.in", "r", stdin);#endif SPEED_UP int t; cin >> t; while (t --) { cin >> n >> m; rep(i, 1, n) cin >> ai[i].l >> ai[i].r; rep(i, 1, m) cin >> bi[i].l >> bi[i].r; countor = 0; Hash.clear(); cnt_a.clear(); cnt_b.clear(); gao(1, ai, cnt_a); gao(1, bi, cnt_b); LL ans = 0; for (map<int, int>::iterator it = cnt_a.begin(); it != cnt_a.end();++ it) if (cnt_b.count(it->first)) ans += it->second * 1ll * cnt_b[it->first]; cout << ans << endl; } return 0;}
0 0
- zoj 3602 Count the Trees(二叉树性质)
- ZOJ 3602Count the Trees(二叉树)
- ZOJ 3602 Count the Trees 树的同构 (哈希)
- ZOJ 3602Count the Trees
- ZOJ 3602 Count the Trees
- ZOJ 3602 Count the Trees
- ZOJ 3602Count the Trees
- zoj 3602 Count the Trees 树的同构
- ZOJ--3602--Count the Trees【DFS+Hash】树的同构
- zoj 3602 Count the Trees (dfs+哈希)
- zoj 3602 Count the Trees 二叉树HASH 浙江省第9届大学生程序设计竞赛 第三题
- (卡特兰数,二叉树数量)How Many Trees? &&Count the Trees -- HDOJ
- zoj S-Trees 1150 (二叉树模拟)
- UVA 10007Count the Trees (dp)
- Count the Trees(卡特蘭數)
- UVa10007 - Count the Trees
- Count the Trees
- Count the Trees
- poj 3083 Children of the Candy Corn(bfs+dfs)
- 购物网站被劫持跳转返利推广网站的分析过程和解决方法
- 常用的转义字符
- 启动此博客的声明
- 几种UML关系
- zoj 3602 Count the Trees(二叉树性质)
- 题目1155:鸡兔同笼
- hashSet 原理
- [leetcode][DP]Maximum Subarray
- 大雨倾盆,思绪纷飞
- Codeforces Round #187 (Div. 2) B. Sereja and Array
- Core Text
- Redis数据结构(二)字典
- maven环境搭建问题