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+ ...
随机推荐
- HDU 2874 Connections between cities(LCA)
题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...
- java常用组件
一.Jpanel 1.面板:容器类组件 2.用途:与Layout配合使用,JFrame—>JPanel—>Layout 二.JTextField 1.文本框 2.JPasswordFiel ...
- SpringBoot系列之(一)helloworld!
要说什么最流行,现阶段肯定是Springboot和Springcloud,在Spring官方网站上第一个就是springboot,可见对springboot的重视程度.主要原因springboot集成 ...
- 结构体和类中属性定义需要static地方
private function Readxxx:Integer;static; public class property XXX:Integer read ReadXXx; Txxx =recor ...
- sql标准支持了事务隔离级别
事务隔离级别 尽管数据库为用户提供了锁的DML操作方式,但直接使用锁管理是非常麻烦的,因此数据库为用户提供了自动锁机制.只要用户指定会话的事务隔离级别,数据库就会分析事务中的SQL语句,然后自动为事务 ...
- 什么叫PV,UV,PR值
1.PV PV(page view),即页面浏览量:用户每1次对网站中的每个网页访问均被记录1次.用户对同一页面的多次访问,访问量累计. 2.什么是UV uv(unique visitor),指访问某 ...
- Keras学习
参加比赛用到了keras,虽然之前用tensorflow,但是感觉tensorflow的确不太友好,api比较难读,然后就学习keras使用 随着深入,发现keras的api确实比较友好 跑了一些ex ...
- 启动eclipse时出现“Failed to load the JNI shared library jvm.dll”错误及解决-及eclipse版本查看
启动eclipse时出现“Failed to load the JNI shared library jvm.dll”错误及解决-及eclipse版本查看 学习了:https://www.cnblog ...
- Qt跨平台的一个例程
我的同事penk在近期北京的Hackathon展示了一个在多平台的例程. 非常多开发人员对这个挺感兴趣的. 今天我就把这个资源介绍给大家. 这是同一个用Qt写的应用.能够同一时候在Ubuntu Des ...
- xterm.js 基于websocket 链接容器 命令行工具
<template> <div> <el-dialog title="命令" :visible.sync="dialogTableVisib ...