关于仙人掌的同构,主要是我太蒟蒻了QAQ,问了好几位大佬才弄好。

手撕仙人掌,你得先有手套 ,你得先了解以下基本知识

a.点双连通分量,没什么好说得,仙人掌上有环,判环用点双

b.树的hash点这里

c.仙人掌点这里                              

对于一棵仙人掌,我们通过一些方法来简化:                 

我们最讨厌的是环,假如说没有环,那么树的hash还是蛮简单的。          

OK那么就是圆方树了,如果你还不知道什么是圆方树,请自行百度或者点这里。

当然对于判定仙人掌的同构,不需要一颗完整的圆方树,只需要在环中间建点

单纯地表示一个环,使用最小表示法(这部分内容我稍后补充QAQ)

单纯地表示一棵树,hash。

嗯,如果你觉得我上面说得十分模糊,那么是正确的,因为还没有开始呢  ^_^

(我其实是不会告诉你我没有用tarjan和完整的圆方树的)

e.g.一个只有一个环的仙人掌

假设我们是这样搜索的

那么在dfs的过程中,要判定一个点在不在环上,记录它的父节点这样在回溯到4时,发现他有一个不是自己儿子的子节点(9)

哈,那就是环的另一端了。

这时抓出9,一直回溯父亲直到4被枚举到,整个环就被揪了出来。

新建一个节点,(在圆方树里则称方点)依次连边。

而对于非环上边直接连就好了

像这样就建立了一个独立出原图的树(一位大佬是这样描述的)

多么优雅的一棵树

好了,现在它是无根的找一下树的重心

新建一个根节点hash一下就好了^_^

你真的觉得这就完了

你太天真了

环是不能这样子搞的

正确打开方式:

别忘了真正的环上点是有顺序的

假如说这个环的方点(中间那个新建点)是根,跑最小表示法(然而我代码里没有这一项QAQ我改天加上)

对于一个普通树节点比如说这样

这样就可以了

代码实现

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned long long ult;
const ult seed1=1324983271ull;
const ult seed2=4327894239ull;
const ult lth1=9301248721ull;
const ult lth2=8317498371ull;
int nt,mt;
bool cmp(ult x,ult y)
{
return x<y;
}
struct pnt{
int hd;
int sh;
int fa;
int wgt;
bool ong;
bool vis;
bool chkd;
ult has;
};
struct ent{
int twd;
int lst;
};
struct Cactus{
pnt p[];
ent e[];
ent r[];
ult st[];
int rt[];
int n,m;
int sqn;
int cnt;
int cmt;
void e_ade(int f,int t)
{
cnt++;
e[cnt].twd=t;
e[cnt].lst=p[f].hd;
p[f].hd=cnt;
}
void r_ade(int f,int t)
{
cmt++;
r[cmt].twd=t;
r[cmt].lst=p[f].sh;
p[f].sh=cmt;
}
void tr_dfs(int x,int f)
{
p[x].vis=true;
for(int i=p[x].hd;i;i=e[i].lst)
{
int to=e[i].twd;
if((i^f)==)continue;
if(!p[to].vis)
{
p[to].fa=x;
p[x].ong=false;
tr_dfs(to,i);
if(!p[x].ong)
{
r_ade(x,to);
r_ade(to,x);
}
}else{
if(p[to].chkd)continue;
sqn++;
int u=x;
for(u=x;;u=p[u].fa)
{
r_ade(n+sqn,u);
r_ade(u,n+sqn);
p[u].ong=true;
if(u==to)break;
}
}
}
p[x].chkd=true;
}
void gravity(int x,int f)
{
p[x].wgt=;
bool fl=true;
for(int i=p[x].sh;i;i=r[i].lst)
{
int to=r[i].twd;
if(to==f)continue;
gravity(to,x);
p[x].wgt+=p[to].wgt;
if(p[to].wgt*>sqn+n)
fl=false;
}
if((sqn+n-p[x].wgt)*>sqn+n)
fl=false;
if(fl)
{
if(rt[])
{
rt[]=x;
}else{
rt[]=x;
}
}
}
void Hash(int x,int f)
{
int top=;
for(int i=p[x].sh;i;i=r[i].lst)
{
int to=r[i].twd;
if(to==f)continue;
Hash(to,x);
}
if(x<=n)
{
for(int i=p[x].sh;i;i=r[i].lst)
{
int to=r[i].twd;
if(to==f)continue;
st[++top]=p[to].has;
}
sort(st+,st+top+,cmp);
p[x].has=seed1;
for(int i=;i<=top;i++)
{
p[x].has=((p[x].has*lth1+st[i])^st[i])+st[i];
}
}else{
int i;
for(i=p[x].sh;i;i=r[i].lst)
{
if(r[i].twd==f)
break;
}
for(i=r[i].lst;i;i=r[i].lst)
{
int to=r[i].twd;
st[++top]=p[to].has;
}
for(i=p[x].sh;i;i=r[i].lst)
{
int to=r[i].twd;
if(to==f)
break;
st[++top]=p[to].has;
}
ult tmp1=seed2,tmp2=seed2;
for(i=;i<=top;i++)
{
tmp1=((tmp1*lth2+st[i])^st[i])+st[i];
}
for(i=top;i;i--)
{
tmp2=((tmp2*lth2+st[i])^st[i])+st[i];
}
p[x].has=min(tmp1,tmp2);
p[x].has*=lth2;
}
}
ult solve(int nn,int mm)
{
n=nn;
m=mm;
cnt=;
for(int i=;i<=m;i++)
{
int x;
int y;
scanf("%d%d",&x,&y);
e_ade(x,y);
e_ade(y,x);
}
tr_dfs(,);
gravity(,);
r_ade(,rt[]);
if(rt[])
{
r_ade(,rt[]);
for(int i=p[rt[]].sh;i;i=r[i].lst)
{
if(r[i].twd==rt[])
r[i].twd=;
}
for(int i=p[rt[]].sh;i;i=r[i].lst)
{
if(r[i].twd==rt[])
r[i].twd=;
}
}
Hash(,);
return p[].has;
}
}C[];
int main()
{
scanf("%d%d",&nt,&mt);
if(C[].solve(nt,mt)==C[].solve(nt,mt))
{
printf("YES\n");
}else{
printf("NO\n");
}
return ;
}

大概就是这样了^_^

仙人掌的同构(hash)的更多相关文章

  1. BZOJ4337: BJOI2015 树的同构(hash 树同构)

    题意 题目链接 Sol 树的同构问题,直接拿hash判一下,具体流程大概是这样的: 首先转化为有根树,预处理出第\(i\)棵树以\(j\)为根时的hash值. 那么两个树同构当且仅当把两棵树的hash ...

  2. luogu P5043 【模板】树同构 hash 最小表示法

    LINK:模板 树同构 题目说的很迷 给了一棵有根树 但是重新标号 言外之意还是一棵无根树 然后要求判断是否重构. 由于时无根的 所以一个比较显然的想法暴力枚举根. 然后做树hash或者树的最小表示法 ...

  3. 仙人掌&圆方树学习笔记

    仙人掌&圆方树学习笔记 1.仙人掌 圆方树用来干啥? --处理仙人掌的问题. 仙人掌是啥? (图片来自于\(BZOJ1023\)) --也就是任意一条边只会出现在一个环里面. 当然,如果你的图 ...

  4. bzoj3871: [Neerc2013 C]Cactus Automorphisms || 3899: 仙人掌树的同构

    Description 给定一个N,N<=50 000个节点的仙人掌,其是指每条边最多在一个环中的无向图,求仙人掌有多少种自同构.自同构是指得是图的顶点集合V到V的变换M, 以P1^a1*P2^ ...

  5. BZOJ 4337: BJOI2015 树的同构 树hash

    4337: BJOI2015 树的同构 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4337 Description 树是一种很常见的数 ...

  6. 【NOI2013模拟】坑带的树(仙人球的同构+圆方树乱搞+计数+HASH)

    [NOI2013模拟]坑带的树 题意: 求\(n\)个点,\(m\)条边的同构仙人球个数. \(n\le 1000\) 这是一道怎么看怎么不可做的题. 这种题,肯定是圆方树啦~ 好,那么首先转为广义圆 ...

  7. BZOJ3899 仙人掌树的同构(圆方树+哈希)

    考虑建出圆方树.显然只有同一个点相连的某些子树同构会产生贡献.以重心为根后(若有两个任取一个即可),就只需要处理子树内部了. 如果子树的根是圆点,其相连的同构子树可以任意交换,方案数乘上同构子树数量的 ...

  8. BZOJ4337:[BJOI2015]树的同构(树hash)

    Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...

  9. 刷题总结——树的同构(bzoj4337 树上hash)

    Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...

随机推荐

  1. ORA-01003: no statement parsed

    环境:delphi 5.BDE.oracle10 delphi里面用tStoreProc调用存储过程出现ORA-01003: no statement parsed. 解决方法:tStoreProc. ...

  2. cvBoostStartTraining, cvBoostNextWeakClassifier和 cvBoostEndTraining

    /****************************************************************************************\ * Boostin ...

  3. Armbian hostname and WiFi configuration

    In previous post i have described installation of Armbian on Orange Pi PC Plus. Now is the time for ...

  4. Announcing Zuul: Edge Service in the Cloud--转

    原文地址:http://techblog.netflix.com/2013/06/announcing-zuul-edge-service-in-cloud.html   The Netflix st ...

  5. Dom4j 查找节点或属性

    Dom4j  查找节点或属性 例如 1 查找下面xml中的student节点的age属性, xpathstr="/students/student/@age"; 2 查找下面xml ...

  6. Ubuntu16.04 “有线未托管”有线网络不可用问题解决

    Ubuntu16.04 “有线未托管”问题解决 电脑上安装的Ubuntu16.04 是通过先安装Ubuntu Server后在通过命令 sudo tasksel 安装的Gnome桌面环境,安装完成后发 ...

  7. chmod---变更文件或目录的权限

    chmod命令用来变更文件或目录的权限.在UNIX系统家族里,文件或目录权限的控制分别以读取.写入.执行3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目录的权限, ...

  8. Uva 10081 Tight words (概率DP)

    Time limit: 3.000 seconds Given is an alphabet {0, 1, ... , k}, 0 <= k <= 9 . We say that a wo ...

  9. [React] Validate Custom React Component Props with PropTypes

    In this lesson we'll learn about how you can use the prop-types module to validate a custom React co ...

  10. linux c statfs系统调用

    statfs 系统调用原型: int statfs(const char *path, struct statfs *buf); 參数说明: path : 位于须要查询信息的文件系统的路径名(不是设备 ...