【BZOJ4337】BJOI2015 树的同构 括号序列
【BZOJ4337】BJOI2015 树的同构
Description
Input
Output
Sample Input
4 0 1 1 2
4 2 0 2 3
4 0 1 1 1
4 0 1 2 3
Sample Output
1
3
1
HINT
题解:题意有问题,题中的树同构是无根树的同构。。。
一开始写了个hash,被卡了,于是学了一发高端的做法。
如果是一棵有根树,那么我们从根开始DFS整棵树,将每个节点的入栈和出栈看成左括号和右括号,最终得到一个括号序列即能代表整棵树。
那么无根树呢?以重心为根即可。如果有两个重心呢?各跑一遍,然后取字典序较小的那个即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int n,mn,m,cnt;
int bel[55],siz[55],to[110],next[110],head[55],mx[55];
string f[55],p[55],g[55];
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
inline void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
void dfs(int x,int fa)
{
int i,sum=0;
for(i=head[x];i!=-1;i=next[i]) if(to[i]!=fa) dfs(to[i],x);
for(i=head[x];i!=-1;i=next[i]) if(to[i]!=fa) p[++sum]=f[to[i]];
f[x]="(";
sort(p+1,p+sum+1);
for(i=1;i<=sum;i++) f[x]+=p[i];
f[x]+=")";
}
void getrt(int x,int fa)
{
mx[x]=0,siz[x]=1;
for(int i=head[x];i!=-1;i=next[i]) if(to[i]!=fa) getrt(to[i],x),siz[x]+=siz[to[i]],mx[x]=max(mx[x],siz[to[i]]);
mx[x]=max(mx[x],m-siz[x]);
mn=min(mn,mx[x]);
}
string get()
{
string tmp="";
m=rd();
int i,a;
memset(head,-1,sizeof(head)),cnt=0;
for(i=1;i<=m;i++)
{
a=rd();
if(a) add(a,i),add(i,a);
}
mn=1<<30,getrt(1,0);
for(i=1;i<=m;i++) if(mx[i]==mn)
{
dfs(i,0);
tmp=max(tmp,f[i]);
}
return tmp;
}
int main()
{
n=rd();
int i,j;
for(i=1;i<=n;i++) g[i]=get();
for(i=1;i<=n;i++)
{
for(j=1;j<i;j++) if(g[i]==g[j]) break;
printf("%d\n",j);
}
return 0;
}//4 4 0 1 1 2 4 2 0 2 3 4 0 1 1 1 4 0 1 2 3
【BZOJ4337】BJOI2015 树的同构 括号序列的更多相关文章
- bzoj4337: BJOI2015 树的同构 树哈希判同构
题目链接 bzoj4337: BJOI2015 树的同构 题解 树哈希的一种方法 对于每各节点的哈希值为hash[x] = hash[sonk[x]] * p[k]; p为素数表 代码 #includ ...
- [BZOJ4337][BJOI2015]树的同构(树的最小表示法)
4337: BJOI2015 树的同构 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1023 Solved: 436[Submit][Status ...
- BZOJ4337:[BJOI2015]树的同构(树hash)
Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...
- BZOJ4337: BJOI2015 树的同构(hash 树同构)
题意 题目链接 Sol 树的同构问题,直接拿hash判一下,具体流程大概是这样的: 首先转化为有根树,预处理出第\(i\)棵树以\(j\)为根时的hash值. 那么两个树同构当且仅当把两棵树的hash ...
- bzoj4337: BJOI2015 树的同构
hash大法好 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...
- BZOJ 4337: BJOI2015 树的同构 树hash
4337: BJOI2015 树的同构 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4337 Description 树是一种很常见的数 ...
- BZOJ4337:[BJOI2015]树的同构——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4337 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根, ...
- 4337: BJOI2015 树的同构
题解: 树的同构的判定 有根树从根开始进行树hash 先把儿子的f进行排序 $f[i]=\sum_{j=1}^{k} { f[j]*prime[j]} +num[i]$(我没有仔细想这样是不是树是唯一 ...
- [BJOI2015]树的同构
嘟嘟嘟 判断树的同构的方法就是树上哈希. 如果树是一棵有根树,那么只要从根节点出发dfs,每一个节点的哈希值等于按传统方式算出来的子树的哈希值的结果.需要注意的是,算完子树的哈希值后要先排序再加起来, ...
随机推荐
- DataGridView使用技巧二:设置单元格只读
一.修改ReadOnly属性 1.设置整个DataGridView只读: DataGridView.ReadOnly=true; 此时用户的新增行和删除行操作也被屏蔽了. 2.设置DataGridVi ...
- libmysqlclient16 libmysqlclient-dev
如果提示安装 libmysqlclient16 则用 libmysqlclient-dev 代替之
- jquery easy ui 简单字段选择搜索实现
code <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title& ...
- selenium测试(Java)--告警框处理(十四)
下面代码中介绍了告警框的处理方法 package com.test.alerthandle; import org.openqa.selenium.By; import org.openqa.sele ...
- e658. 组合图形
Area shape = new Area(shape1); shape.add(new Area(shape2)); shape.subtract(new Area(shape3)); shape. ...
- KEGG orthology (KO) 数据库简介
KEGG, 简称京都基因组百科全书,包含了许多的数据库,对于研究基因功能来说,KEGG orthology 数据库是最基本的一个数据库: KEGG Orthology 简称KO, 对于每个功能已知的基 ...
- 【Java面试题】58 char型变量中能不能存贮一个中文汉字?为什么?
char型变量是用来存储Unicode编码的字符的,unicode编码字符集中包含了汉字,所以,char型变量中当然可以存储汉字啦.不过,如果某个特殊的汉字没有被包含在unicode编码字符集中,那么 ...
- 访问GitLab的PostgreSQL数据库
1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...
- c语言的fopen
c语言fopen函数 fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen(文件名,使用文件方式); 其中, “文件指针名”必须是被说明为FILE 类型的指针变量: “文件名” ...
- Javascript继承机制总结 [转]
转自:http://bbs.csdn.net/topics/260051906 Javascript继承 一直想对Javascript再次做一些总结,正好最近自己写了一个小型Js UI库,总结了一下J ...