树hash啊

我的做法很垃圾,就是yy一种只有一个孩子时hash值和孩子一样的hash法

然后用重心去作为根遍历

这样有点问题,就是重心假如也是要删掉的那就gg了

那我们求tot的时候删掉的点就不管直接设为0,求重心的时候也不管这些删掉的点就可以了

其实

先把新的树建出来不好吗

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e4+_;
const int maxt=+; const LL hbase=; LL mi[maxn];
void hyu(){mi[]=;for(int i=;i<maxn;i++)mi[i]=mi[i-]*hbase;} struct TREE
{
int id,ptt;
TREE(){ptt=;}
struct node
{
int x,y,next;
}a[*maxn];int len,last[maxn],du[maxn];
void ins(int x,int y)
{
len++;
a[len].x=x;a[len].y=y;
a[len].next=last[x];last[x]=len; du[x]++;
if(du[x]==)ptt++;
else if(du[x]==)ptt--;
} //---------------------------------def-------------------------------------------- int tot[maxn];
void dfs(int x,int fr)
{
if(du[x]==)tot[x]=;
else tot[x]=;
for(int k=last[x];k;k=a[k].next)
if(a[k].y!=fr)dfs(a[k].y,x),tot[x]+=tot[a[k].y];
}
int G[maxn];
void getrt(int x,int fr)
{
G[x]=;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
G[x]=max(G[x],tot[y]);
if(y!=fr)
{
tot[x]-=tot[y];
tot[y]+=tot[x];
getrt(y,x);
tot[y]-=tot[x];
tot[x]+=tot[y];
}
}
} //----------------------------------getrt-------------------------------------------- int num;LL THash[];
LL h[maxn]; int tlen; LL tt[maxn];
void gethash(int x,int fr)
{
for(int k=last[x];k;k=a[k].next)
if(a[k].y!=fr)gethash(a[k].y,x);
tlen=;
for(int k=last[x];k;k=a[k].next)
if(a[k].y!=fr)tt[++tlen]=h[a[k].y]; if(tlen==)h[x]=;
else
{
sort(tt+,tt+tlen+);
h[x]=(tlen-)*;
for(int i=;i<=tlen;i++)h[x]+=tt[i]*mi[i-];
}
} //---------------------------------gethash------------------------------------------- void main()
{
int n,x,y;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
ins(x,y),ins(y,x);
}
ptt=n-ptt; dfs(,),getrt(,);
num=;
int mn=(<<);
for(int i=;i<=n;i++)
if(du[i]!=)mn=min(mn,G[i]);
for(int i=;i<=n;i++)
if(mn==G[i])
gethash(i,),THash[num++]=h[i];
}
}tr[maxt];
bool check(int x,int y)
{
for(int i=;i<tr[x].num;i++)
for(int j=;j<tr[y].num;j++)
if(tr[x].THash[i]==tr[y].THash[j])return true;
return false;
} int aslen,as[maxt];
bool cmp(int x,int y){return tr[x].ptt<tr[y].ptt;}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int m;
scanf("%d",&m);hyu();
for(int i=;i<=m;i++)
tr[i].id=i,tr[i].main();
for(int i=;i<=m;i++)
{
bool bk=false;
for(int j=;j<=aslen;j++)
if(check(i,as[j])){bk=true;break;}
if(!bk)as[++aslen]=i;
}
sort(as+,as+aslen+,cmp);
printf("%d\n",aslen);
for(int i=;i<aslen;i++)printf("%d ",tr[as[i]].ptt);
printf("%d\n",tr[as[aslen]].ptt); return ;
}

bzoj4474: [Jsoi2015]isomorphism的更多相关文章

  1. 【BZOJ4474】isomorphism(树的同构,哈希)

    题意:一个无向树的度数为 2的结点称为假结点,其它结点称为真结点.一个无向树的简化树其结点由原树的全体真结点组成,两个真结点之间有边当且仅当它们在原树中有边,或者在原树中有一条联结这两个结点的路,其中 ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. 「JSOI2015」isomorphism

    「JSOI2015」isomorphism 传送门 我们还是考虑树哈希来判同构. 但是我们需要使用一些特殊的手段来特殊对待假节点. 由于是无向树,我们首先求出重心,然后以重心为根跑树哈希. 此处我们不 ...

  4. JSOI2015 Round1——完挂

    感觉眼前天地转了转…… Day 0 和zxy,zyh一同坐车去扬中,同行的还有llr 路上zyh基本在睡觉…… 入住的宾馆各种坑爹,同一层住的两个房间一个有网一个没网 我有幸入住了有网的房间,zyh在 ...

  5. BZOJ1478 Sgu282 Isomorphism

    Problem A: Sgu282 Isomorphism Time Limit: 15 Sec  Memory Limit: 64 MBSubmit: 172  Solved: 88[Submit] ...

  6. bzoj4487[Jsoi2015]染色问题 容斥+组合

    4487: [Jsoi2015]染色问题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 211  Solved: 127[Submit][Status ...

  7. BZOJ_4476_[Jsoi2015]送礼物_01分数规划+单调队列

    BZOJ_4476_[Jsoi2015]送礼物_01分数规划+单调队列 Description JYY和CX的结婚纪念日即将到来,JYY来到萌萌开的礼品店选购纪念礼物. 萌萌的礼品店很神奇,所有出售的 ...

  8. Isomorphism 同构

    小结: 1.两个有限维度的向量空间,在同一数域下,是同构的 等价于 它们维数相等. Isomorphism 同构 0.1.8 Isomorphism. If U and V are vector sp ...

  9. 【BZOJ4477】[JSOI2015]字符串树(Trie树)

    [BZOJ4477][JSOI2015]字符串树(Trie树) 题面 BZOJ 题解 对于每个点维护其到根节点的所有字符串构成的\(Trie\),显然可持久化一下就很好写了. 然后每次询问就是\(u+ ...

随机推荐

  1. 信息收集渠道:文本分享类网站Paste Site

    信息收集渠道:文本分享类网站Paste Site Paste Site是一种专门的文本分享的网站.用户可以将一段文本性质的内容(如代码)上传到网站,然后通过链接分享给其他用户.这一点很类似于现在的优酷 ...

  2. 转载:P2P技术原理及应用(1)

    转帖allen303allen的空间 作 者:金海 廖小飞 摘要:对等网络(P2P)有3种主要的组织结构:分布式哈希表(DHT)结构.树形结构.网状结构.P2P技术已 经延伸到几乎所有的网络应用领域, ...

  3. Codeforces 235 C

    题目大意 给定一个模板串, 再给出\(n\)个询问, 询问每一个串的循环串总共在原串中出现了多少次. 循环串: 比如说有\(str[] = \{ABCD\}\), 则其循环串有\(\{ABCD\}, ...

  4. centos 升级内核失败回救

    在升级 centos6.3上使用, yum -y update  ... 灾难出现了!!! 解决方法: 1. 在机器启动的时候, 按F1, 会出现选择内核,选一个原来的. 2. vim /etc/gr ...

  5. 在windows下安装gulp[转]

    一.准备工作 1.什么是 npm? npm 是 nodejs 的包管理工具,主要功能就是管理.更新.搜索.发布node的包.Gulp 就是通过 NPM 安装的.关于 NPM 中文介绍,这里有一篇非常不 ...

  6. HttpClient获取Cookie的两种方式

    转载:http://blog.csdn.net/zhangbinu/article/details/72777620 一.旧版本的HttpClient获取Cookies p.s. 该方式官方已不推荐使 ...

  7. 使用 rman duplicate from active database 搭建dataguard 手记--系列二

    run { allocate channel prmy1 type disk; allocate channel prmy2 type disk; allocate channel prmy3 typ ...

  8. 定义自己的代码风格CheckStyle简单使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-/ ...

  9. error MSB8011: 未能注冊输出。请尝试启用“逐用户重定向”

    初试MFC就遇到困难了.vs2010创建一个MFC ActiveX控件项目. 什么都还没做呢,就出现故障了? 郁闷呀.! ! 1>C:\Program Files\MSBuild\Microso ...

  10. jvm基础(2)

    7.类装载器 (1)class装载验证流程: A加载.这是装载类的第一个阶段,执行的动作包括:取得类的二进制流,转为方法区数据结构,在java堆中生成对应的java.lang.Class对象. B链接 ...