poj 1635 Subway tree systems (树同构)
来源:互联网 发布:软件质量管理的问题 编辑:程序博客网 时间:2024/06/05 03:16
题目描述
传送门
题解
树同构问题。
昨天考试的时候用到了有根树(儿子有顺序)的同构判断。
我的想法是dfs得到树的dfs序,然后将dfs序中的每个位置替换成这个位置的点的儿子数,然后对数列进行hash。这样其实就转换是序列的hash.
但是这道题儿子是没有顺序的,所以如果只是用上面的方式肯定不行。对于每个节点,现将所有儿子的hash进行排序,然后得到的序列再进行hash。一层一层的向上算,最终得到一个hash值。后来查了一下,这种做法貌似与最小表示法比较类似。
代码
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#define N 3003#define ULL unsigned long long #define p 2000001001using namespace std;int tot,nxt[N],point[N],v[N];ULL mi[N],ans[N];int n,m,T,st[N],top,fa[N],cnt[N],belong[N]; char ch[N];void add(int x,int y){ tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;// cout<<x<<" "<<y<<endl;}void dfs(int x){ cnt[x]=1; vector<ULL> tmp; int k=0; tmp.clear(); for (int i=point[x];i;i=nxt[i]) { dfs(v[i]); cnt[x]++; tmp.push_back(ans[v[i]]); } sort(tmp.begin(),tmp.end()); ans[x]=0; k=tmp.size(); for (int i=0;i<tmp.size();i++) ans[x]+=mi[i+1]*tmp[i]; if (!k) ans[x]=cnt[x];}ULL solve(){ memset(point,0,sizeof(point)); tot=0; n=1; memset(cnt,0,sizeof(cnt)); scanf("%s",ch+1); int len=strlen(ch+1); int t=1; for (int i=1;i<=len;i++) if (ch[i]=='0') { ++n; fa[n]=t; add(t,n); t=n; }else t=fa[t]; top=0; dfs(1); return ans[1];}int main(){ freopen("a.in","r",stdin);// freopen("my.out","w",stdout); scanf("%d",&T); mi[0]=1; for (int i=1;i<=3000;i++) mi[i]=mi[i-1]*p; while (T--) { if (solve()==solve()) printf("same\n"); else printf("different\n"); }}
阅读全文
0 0
- poj 1635 Subway tree systems(树的同构,经典)
- poj 1635 Subway tree systems (树同构)
- poj 1635 Subway tree systems 判断是否是同构树
- poj 1635 Subway tree systems 判断树的同构 树的最大最小表示法模板
- poj-1635 Subway tree systems(判断两个有根树是否同构)-哈希法
- POJ 1635 Subway tree systems(HASH+判断两棵有根树是否同构)
- pku 1635 Subway tree systems(树的同构,最小表示)
- Poj 1635 Subway tree systems (树的最小表示)
- POJ 1635 Subway tree systems
- poj 1635 Subway tree systems
- poj 1635 Subway tree systems
- POJ 1635 Subway tree systems
- poj 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- HDU 1954Subway tree systems(树的同构,树的最小表示)
- iOS-数据存储方式四之Core Data
- Java hashcode方法编写技巧 —— 记住这3条约定
- web前端之廖雪峰js学习记录
- Kotlin编程之扩展方法
- 【asp.net】邮件发送
- poj 1635 Subway tree systems (树同构)
- Sicily 1211. 商人的宣传
- 使用Redis做缓存——个人笔记
- Maven——maven工程找不到maven库
- Problem A: 大整数的加法运算
- [JSON]2017年最新县及县以上行政区划代码
- sun.misc.BASE64Encoder找不到jar包的解决方法
- MySQL 选择数据库
- HTTP cookies