同构树的判断 poj 1635

2024-05-07 08:38
文章标签 判断 poj 1635 构树

本文主要是介绍同构树的判断 poj 1635,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目的描述比较长,总的意思就是给出两棵有根树,判断它们是不是同构树。

所谓的同构树,定义我也不太知道尴尬。按字面上的意思就是两棵结构相同的树。

如第一棵树和第二棵树就是同构树,它们和第三棵树不是同构树:


并且,同构树它们有一一对应的点。

对于任意一棵有根树,都可以用括号表示法来表示,可以去http://www.byvoid.com/blog/directed-tree-bracket-sequence/看看。

如上面的第一棵树,既可以表示成(()(())())(()),也可以表示成(())((())()()),有很多的表示方法,为了判断,我们要用最小表示法,就是字典序最小的一个。第一种括号可以分解成:


每一层都排一下序就可以找到字典树最小的一个啦吐舌头

其实原题就是这样的一个括号序列,0代表左括号,1代表右括号。这样问题就很简单了,把这读入的两个字符串当做括号处理,如果它们的最小表示法是一样的,那么就是同构树啦。

但是,我们要对字符串进行分解,分解完了还要排序,不仅时间复杂度不乐观,空间处理也比较麻烦。所以,我们可以用哈希,把一个括号序列哈希掉。合并的时候将哈希值从小到大排序,再合成当前的哈希值。可以去看看《杨弋<Hash在信息学竞赛中的一类应用>》这篇文章。

我看到网上有一种方法,判断每一层的节点数是否相等,且相同层数子树大小经过排序后对应相等,那么就是同构。我不知道这种方法对不对,网上有的说这是错的。我写了一下,还真的过了,求证明。

#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;int n, P[3007];
bool d[6007];// 这样读入会快些?? 
int read(int i) {char ch = '*';do ch = getchar(); while (ch < '0' || ch > '1');for (; ch == '0' || ch == '1'; ch = getchar()) d[i ++] = ch == '1';return i;
}// 字符串的[L, R)的哈希值 
int GetHash(int L, int R) {if (L >= R) return 1;// 只能用vector??我想不到更好的办法 vector<int> t;t.clear();int f = 0, cnt = 0;for (int i = L; i < R; i ++) {if (d[i]) f --; else f ++;// 当括号匹配时就可以分解了 if (f == 0) {t.push_back(GetHash(L + 1, i)), cnt ++;L = i + 1;}}// 排序 sort(t.begin(), t.end());// 求出当前哈希值 int res = 1;// 这要找个好的哈希函数,// 一开始我写得不好,有很多重复// 后来在网上看到如下的方法 for (int i = 0; i < cnt; i ++)res = (res ^ t[i]) * P[i] % 15237;return res;
}int main() {freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);srand((int)time(0));for (int i = 0; i < 3000; i ++) P[i] = rand() % 15237;int T;scanf("%d\n", &T);while (T --) {int n = read(0);int m = read(n);if (GetHash(0, n) == GetHash(n, m)) printf("same\n");else printf("different\n");}return 0;
}

奇怪的方法(代码有点烂, 其实不用构图的):

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int N = 3007;struct Tree {int n;int father[N], head[N], to[N], next[N], cnt;struct data {int depth, size;bool operator < (data const &o) const {if (depth != o.depth) return depth < o.depth;return size < o.size;}bool operator == (data const &o) const {return depth == o.depth && size == o.size;}}d[N];void Insert(int u, int v) {to[++ cnt] = v;next[cnt] = head[u];head[u] = cnt;}int Q[N];void Build(char *s) {cnt = 0;memset(head, 0, sizeof(head));int len = strlen(s);n = 1;int cur = 1;for (int i = 0; i < len; i ++)if (s[i] == '0') {Insert(cur, ++ n);father[n]= cur;cur = n;}else cur = father[cur];d[1].depth = 1;int lo = 0, hi = 0;Q[0] = 1;for (; lo <= hi; lo ++) {int u = Q[lo];for (int e = head[u]; e; e = next[e]) {int v = to[e];d[v].depth = d[u].depth + 1;Q[++ hi] = v;}}for (; hi >= 0; hi --) {int u = Q[hi];d[u].size = 1;for (int e = head[u]; e; e = next[e]) d[u].size += d[to[e]].size;}sort(d + 1, d + 1 + n);}bool operator == (Tree const &o) const {if (n != o.n) return false;for (int i = 1; i <= n; i ++)if (!(d[i] == o.d[i])) return false;return true;}
}a, b;char dat[N];int main() {int T;scanf("%d\n", &T);while (T --) {scanf("%s\n", dat);a.Build(dat);scanf("%s\n", dat);b.Build(dat);if (a == b) printf("same\n");else printf("different\n");}return 0;
}


这篇关于同构树的判断 poj 1635的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966859

相关文章

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

如何测试计算机的内存是否存在问题? 判断电脑内存故障的多种方法

《如何测试计算机的内存是否存在问题?判断电脑内存故障的多种方法》内存是电脑中非常重要的组件之一,如果内存出现故障,可能会导致电脑出现各种问题,如蓝屏、死机、程序崩溃等,如何判断内存是否出现故障呢?下... 如果你的电脑是崩溃、冻结还是不稳定,那么它的内存可能有问题。要进行检查,你可以使用Windows 11

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D