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+ ...
随机推荐
- LibieOJ 6170 字母树 (Trie)
题目链接 字母树 (以每个点为根遍历,插入到trie中,统计答案即可)——SamZhang #include <bits/stdc++.h> using namespace std; #d ...
- 洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would li ...
- java三角形和菱形的打印
一.三角形的打印 package 向家康; import java.util.Scanner; public class Main { public void san(int num) { for(i ...
- sqlalchemy如何实现时间列自动更新?
目标:数据表的时间列在其他列内容更新的时候,自动更新时间列到更新的时间 方法:数据库表模型如下:server_default表示初始时间,onupdate表示更新的时间 class MonitorDa ...
- paramiko执行命令超时的问题
问题:paramiko远程执行命令,需要等到命令返回信息,如果命令执行时间比较长,返回信息就需要等很久 方案:1.使用nohup + 待执行命令 + & ,使用后台执行的方式,应该可以快速返回 ...
- Android基础新手教程——3.7 AnsyncTask异步任务
Android基础新手教程--3.7 AnsyncTask异步任务 标签(空格分隔): Android基础新手教程 本节引言: 本节给大家带来的是Android给我们提供的一个轻量级的用于处理异步任务 ...
- CSS规则的优先级匹配
CSS规则之间能够互相覆盖.这一点我们应该已经习以为常了.然而正是因为规则之间能够互相覆盖.子元素继承父元素的默认行为,导致了CSS冲突的问题. 碰到CSS冲突时.通常我们会增加一些更加具体的规则来明 ...
- window.open 打开子窗体,关闭全部的子窗体
需求:通过window.open方法打开了子窗体,当关闭主窗体时.子窗体应当也关闭. 实现思路: 1.打开子窗体函数window.open(url,winName)的第二个參数winName能够唯一标 ...
- php自定义错误
function myErrorHandler ( $errno , $errstr , $errfile , $errline ) { if (!( error_reporting () & ...
- Java集合01----ArrayList的遍历方式及应用
Java集合01----ArrayList的遍历方式及应用 前面已经学习了ArrayList的源代码,为了学以 ...