链接

http://poj.org/problem?id=1635

题意


判断两个有根树是否同构。


解析


判断两个有根树是否同构,本质上是hash应用,每个点的权值是这个子树的权值和。树上某一组合不同都会导致最终结构的不同。


代码

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int maxn = 3000+10;
typedef long long LL;
vector<int>G1[maxn];
vector<int>G2[maxn];
int p1[maxn], p2[maxn];
struct hash{int length;LL value;hash():length(0), value(0){}hash(char c):length(1), value(c){}hash(int l, LL v):length(l), value(v){}bool operator < (const hash &a) const{return value<a.value;}
};
const LL magic = 321;
LL pow(LL a, int b){LL res = 1;while (b) {if (b & 1) res *= a;a *= a;b>>=1;}return res;
}hash operator + (const hash &a, const hash &b)
{return hash(a.length+b.length, a.value*pow(magic, b.length)+b.value);
}void operator += (hash &a, const hash &b){a = a+b;
}vector<hash>child[maxn];
hash dfs(int pre, int cur, vector<int>G[maxn]) {hash ret;child[cur].clear();for (int i=0; i<G[cur].size(); i++) {if (G[cur][i] == pre)continue;child[cur].push_back(dfs(cur, G[cur][i], G));}sort(child[cur].begin(), child[cur].end());for (int i=0; i<child[cur].size(); i++) ret += child[cur][i];ret = '(' + ret + ')';return ret;
}LL gethash(int root, vector<int>m[maxn]) {return dfs(-1, root, m).value;
}
int main() {int T;scanf("%d", &T);while (T--) {char s[maxn];scanf("%s", s);int slen = strlen(s);int t=0;int now = 0;for (int i=0; i<slen; i++)G1[i].clear(), G2[i].clear();for (int i=0; i<slen; i++) {if (s[i] == '1')now = p1[now];else{t++;G1[now].push_back(t);p1[t] = now;now = t;}}char p[maxn];scanf("%s", p);int plen = strlen(p);t = 0;now = 0;for (int i=0; i<plen; i++) {if (p[i] == '1') now = p2[now];else {t++;G2[now].push_back(t);p2[t] = now;now = t;}}if (slen != plen) {puts("differrent");continue;}LL tree1 = gethash(0, G1);LL tree2 = gethash(0, G2);if (tree1 == tree2)puts("same");elseputs("different");}return 0;
}

树的同构 poj 1635相关推荐

  1. POJ 1635 Subway tree systems 树的Hash 或 树的最小表示法

    题目大意: 就是给出从树的中心开始的dfs序, 根据两个dfs序列判断两棵树是否同构 大致思路: 首先根据dfs一直是从树的中心开始的, 所以不用担心中心的问题, 用树的Hash的话当然可以做 另外一 ...

  2. [BJOI2015]树的同构

    嘟嘟嘟 判断树的同构的方法就是树上哈希. 如果树是一棵有根树,那么只要从根节点出发dfs,每一个节点的哈希值等于按传统方式算出来的子树的哈希值的结果.需要注意的是,算完子树的哈希值后要先排序再加起来, ...

  3. PTA 03-树1 树的同构 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...

  4. [BJOI2015] 树的同构

    4337: BJOI2015 树的同构 Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 1092  Solved: 460 [Submit][Stat ...

  5. 树根c语言,03-树1 树的同构 (C语言链表实现)

    #include #include #include #include typedef char ElemType; typedef struct BinTree { ElemType data; s ...

  6. 树的同构(c语言静态链表实现)

    题目 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子 ...

  7. 【视频讲解】基础实验4-2.1 树的同构 (25 分)

    立志用最少的代码做最高效的表达 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树 ...

  8. 7-3 树的同构 (25 分)(思路加详解)来呀baby!!!!!!!!

    一:题目 7-3 树的同构 (25 分) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把 ...

  9. 树的同构模板题(法1.最小表示法+法2.树哈希)

    树的同构 problem solution code solution code problem 模板题 solution Ⅰ. 最小表示法 将树转化为 0/10/10/1 括号序列:从根开始 dfs ...

最新文章

  1. servlet Context (在servlet 中设定context)
  2. 心脏病预测模型(基于Python的数据挖据)
  3. dataframe 查找特定值_省时省力的查找引用函数
  4. 目标检测 RCNN算法详解
  5. Docker最佳实践-部署LNMP环境
  6. ALV中调用Excel inplace时没能传递数据实例解决
  7. 教你用Python合成人像,足不出户游遍全球!
  8. Redis——史上最强【集群】入门实践教程
  9. 疑似华为P50系列7月29日发布:麒麟9000旗舰芯片加持
  10. HDU1850 Being a Good Boy in Spring Festival【Nim博弈】
  11. 20200529每日一句
  12. javaSE基础篇之char
  13. vue3安装WangEditor富文本编辑器v5版本
  14. WPS2019专业版,最新免费高级版
  15. Java二叉树的最大深度
  16. 苹果a7处理器_苹果历代cpu性能对比
  17. SQL Server evaluation period has expired
  18. 再忆年少,再见年少——青春路上的我们
  19. 逆波兰式 java_逆波兰式(后缀表达式)的计算 中缀表达式转后缀表达式(逆波兰式)【java实现】...
  20. 服务更新发布方式------“金丝雀、滚动更新、蓝绿部署”

热门文章

  1. 微信小程序 视频列表滑动无限循环(仿抖音)
  2. Nature reviews Neurology:癫痫合并神经行为障碍:基于网络的精确分类
  3. 注册Apple ID
  4. this和this.$router这个方法在setup()里使用竟然是undefined----使用composition-api踩到的坑总结篇
  5. ltunes无法验证服务器,itunes无法验证服务器身份
  6. benchmark TPC-H postgreSQL
  7. vim 录制宏,自动循环执行组合操作
  8. 五个温度带的分界线_初中地理知识点:我国的温度带
  9. 伽罗华域(Galois Field)有限域元素生成和运算原理
  10. axure8 事件改变样式_【Petrel老师带你玩转Axure 8(二)】Axure部件样式与页面样式管理...