bzoj4474: [Jsoi2015]isomorphism
树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的更多相关文章
- 【BZOJ4474】isomorphism(树的同构,哈希)
题意:一个无向树的度数为 2的结点称为假结点,其它结点称为真结点.一个无向树的简化树其结点由原树的全体真结点组成,两个真结点之间有边当且仅当它们在原树中有边,或者在原树中有一条联结这两个结点的路,其中 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 「JSOI2015」isomorphism
「JSOI2015」isomorphism 传送门 我们还是考虑树哈希来判同构. 但是我们需要使用一些特殊的手段来特殊对待假节点. 由于是无向树,我们首先求出重心,然后以重心为根跑树哈希. 此处我们不 ...
- JSOI2015 Round1——完挂
感觉眼前天地转了转…… Day 0 和zxy,zyh一同坐车去扬中,同行的还有llr 路上zyh基本在睡觉…… 入住的宾馆各种坑爹,同一层住的两个房间一个有网一个没网 我有幸入住了有网的房间,zyh在 ...
- BZOJ1478 Sgu282 Isomorphism
Problem A: Sgu282 Isomorphism Time Limit: 15 Sec Memory Limit: 64 MBSubmit: 172 Solved: 88[Submit] ...
- bzoj4487[Jsoi2015]染色问题 容斥+组合
4487: [Jsoi2015]染色问题 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 211 Solved: 127[Submit][Status ...
- BZOJ_4476_[Jsoi2015]送礼物_01分数规划+单调队列
BZOJ_4476_[Jsoi2015]送礼物_01分数规划+单调队列 Description JYY和CX的结婚纪念日即将到来,JYY来到萌萌开的礼品店选购纪念礼物. 萌萌的礼品店很神奇,所有出售的 ...
- Isomorphism 同构
小结: 1.两个有限维度的向量空间,在同一数域下,是同构的 等价于 它们维数相等. Isomorphism 同构 0.1.8 Isomorphism. If U and V are vector sp ...
- 【BZOJ4477】[JSOI2015]字符串树(Trie树)
[BZOJ4477][JSOI2015]字符串树(Trie树) 题面 BZOJ 题解 对于每个点维护其到根节点的所有字符串构成的\(Trie\),显然可持久化一下就很好写了. 然后每次询问就是\(u+ ...
随机推荐
- 洛谷—— P2895 [USACO08FEB]流星雨Meteor Shower
P2895 [USACO08FEB]流星雨Meteor Shower 题目描述 Bessie hears that an extraordinary meteor shower is coming; ...
- 转载:P2P技术原理及应用(1)
转帖allen303allen的空间 作 者:金海 廖小飞 摘要:对等网络(P2P)有3种主要的组织结构:分布式哈希表(DHT)结构.树形结构.网状结构.P2P技术已 经延伸到几乎所有的网络应用领域, ...
- eclipse项目java版本更改
然后.右键点击项目->properties->Java Compiler->....如图 最后,右键点击项目->properties->Project Facets ...
- Android在其他线程中更新UI
public class TransferTools { private static final int MSG_START = 1001; private static final int MSG ...
- Delphi TScrollBar 用于滚动窗口、组件内容
滚动条组件(TScrollBar)此组件是一个Windows滚动条,用于滚动窗口.组件内容.许多控制有滚动条属性,它们把滚动条作为自己的一部分,对于没有完整滚动条的控制,TScrollBar组件提供了 ...
- go mysql dsn
https://github.com/go-sql-driver/mysql#dsn-data-source-name DSN (Data Source Name) The Data Source N ...
- windows下redis安装以及简单配置
1.下载redis 下载地址https://github.com/dmajkic/redis/downloads.有32bit和64bit根据自己需要选择就可以了. 2.安装redis 首先使用cmd ...
- Spring学习之事务注解@Transactional
今天学习spring中的事务注解,在学习Spring注解事务之前需要明白一些事务的基本概念: 事务:并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通 ...
- ubuntu14.04下CPU的caffe配置,不成功的朋友请与我(lee)联系,后面附带邮箱
因广大朋友需求cpu的caffe配置.所以我(lee)在这份博客中对cpu配置caffe做出对应操作说明.希望能够解决大家对cpu配置caffe的困惑.少走弯路. 假设有安装不成功的朋友能够和我联系, ...
- WPF02(concept)
(转自http://www.cnblogs.com/huangxincheng/archive/2012/06/17/2552322.html)这些天从项目上接触到了wpf,感觉有必要做一个笔记,首篇 ...