【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,每一个节点的哈希值等于按传统方式算出来的子树的哈希值的结果.需要注意的是,算完子树的哈希值后要先排序再加起来, ...
随机推荐
- Hibernate- HQL查询方式
HQL查询方式 01.基本查询 02.动态实例查询 03.分页查询 04.条件查询 05.连接查询 06.子查询
- 使用MultipartEntity上传文件(带进度对话框)
package com.home.uploadfile; import java.io.File; import android.app.Activity; import android.os.Bun ...
- JS中的数字比较
if(parseInt(current_index)!=parseInt(uls)){
- [从jQuery看JavaScript]-匿名函数与闭包
jQuery片段: (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的第一眼,我 ...
- Web下的整体测试 --性能测试及优化思路
随着Internet的日益普及,现在基于B/S结构的大型应用越来越多,可如何对这些应用进行测试成为日益迫切的问题.有许多测试人员来信问我B/S的测试如何做,由于工作较繁忙,对大家提出的问题也是头痛医头 ...
- 消息中间件系列之Java API操作ActiveMQ
一.依赖 <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activem ...
- div 点击展开
<script type="text/javascript" src="http://libs.baidu.com/jquery/2.1.1/jquery.min. ...
- 关于C++输出中文乱码的解决方案
把页面编码转换为UTP-8的编码 1.打开G:\vs2013way\VC\vcprojectitems目录 在 file.h newc++file.cpp 中写两句话 #pragma once#pra ...
- HTTP/1.1 学习
发现对于HTTP协议不能脱口而出,故而怒翻资料,RFC2616 . 在其abstract中是这么说HTTP的,应用层协议,generic.无状态.其特点之一是 the typing and negot ...
- Python3.4下使用sqlalchemy
一. 1.用sudo apt-get install python3-numpy之后,会默认把numpy安装到 /usr/lib/python3/dist-packages目录下,而且版本比较低. ...