【BZOJ4474】isomorphism(树的同构,哈希)
题意:一个无向树的度数为 2的结点称为假结点,其它结点称为真结点。一个无向树的简化树
其结点由原树的全体真结点组成,两个真结点之间有边当且仅当它们在原树中有边,或者在
原树中有一条联结这两个结点的路,其中间节点全是假结点。两个无向树各自的简化树如果
同构,即存在结点之间的一一对应,使得在一个树中的任意两个结点之间有边当且仅当它们
的对应结点在另一个树中有边,则称原来的两个无向树实质同构。给定若干个无向树,将相
互实质同构的无向树只保留一个其余删除。统计剩下的相互不实质同构的无向树个数,并将
它们的简化树结点个数从小到大输出。
cas<=20 n<=10000
思路:把很久以前在另一个地方写的题解搬过来
定义题。做法请看题目。
删除假节点并连边:先按输入构图并统计每个点的度数 找一个度不为2的点做一遍dfs
if d[v]=2 f[find(u)]=f[find(v)]
做完之后重连所有f[x[i]]<>f[y[i]]的边
同构:
哈希,将点上的哈希初值都=1,对于点u,取与u相连的点权V并排序,随意哈希作为新的哈希值。迭代10000000000000000000000000000000次。最后只需判断哈希值是否相等。
- const mo=;
- var head,vet,next,d,b,x,y,e1,q,f:array[..]of longint;
- hash,s,c,h:array[..]of int64;
- cas,v1,e,v,i,tot,len,n,j,k,ans:longint;
- tmp:int64;
- p:boolean;
- procedure qsort(l,r:longint);
- var i,j:longint;
- t,mid:int64;
- begin
- i:=l; j:=r; mid:=q[(l+r)>>];
- repeat
- while mid>q[i] do inc(i);
- while mid<q[j] do dec(j);
- if i<=j then
- begin
- t:=q[i]; q[i]:=q[j]; q[j]:=t;
- inc(i); dec(j);
- end;
- until i>j;
- if l<j then qsort(l,j);
- if i<r then qsort(i,r);
- end;
- function find(k:longint):longint;
- begin
- if f[k]<>k then f[k]:=find(f[k]);
- find:=f[k];
- end;
- procedure add(a,b:longint);
- begin
- inc(tot);
- next[tot]:=head[a];
- vet[tot]:=b;
- head[a]:=tot;
- end;
- procedure dfs(u,fa:longint);
- var e,v:longint;
- begin
- e:=head[u];
- while e<> do
- begin
- v:=vet[e];
- if v<>fa then
- begin
- if d[v]= then f[find(v)]:=f[find(u)];
- dfs(v,u);
- end;
- e:=next[e];
- end;
- end;
- begin
- readln(cas);
- for v1:= to cas do
- begin
- read(n);
- fillchar(head,sizeof(head),);
- fillchar(b,sizeof(b),);
- fillchar(d,sizeof(d),);
- for i:= to n do hash[i]:=;
- for i:= to n do f[i]:=i;
- tot:=;
- for i:= to n- do
- begin
- read(x[i],y[i]);
- inc(d[x[i]]); inc(d[y[i]]);
- add(x[i],y[i]);
- add(y[i],x[i]);
- end;
- for i:= to n do
- if d[i]<> then
- begin
- dfs(i,-);
- break;
- end;
- tot:=;
- fillchar(head,sizeof(head),);
- for i:= to n- do
- if f[x[i]]<>f[y[i]] then
- begin
- b[f[x[i]]]:=; b[f[y[i]]]:=;
- add(f[x[i]],f[y[i]]);
- add(f[y[i]],f[x[i]]);
- end;
- for i:= to n do
- if d[i]<> then inc(e1[v1]);
- for i:= to do
- begin
- for j:= to n do h[j]:=hash[j];
- for j:= to n do
- if b[j]= then
- begin
- e:=head[j];
- len:=;
- while e<> do
- begin
- v:=vet[e];
- inc(len); q[len]:=h[v];
- e:=next[e];
- end;
- if len> then qsort(,len);
- tmp:=;
- for k:= to len do tmp:=(tmp*+q[k]) mod mo;
- hash[j]:=tmp;
- end;
- end;
- len:=;
- for i:= to n do
- if b[i]= then begin inc(len); q[len]:=hash[i]; end;
- if len> then qsort(,len);
- tmp:=;
- for i:= to len do tmp:=(tmp*+q[i]) mod mo;
- s[v1]:=tmp;
- end;
- for i:= to cas do
- begin
- p:=true;
- for j:= to i- do
- if s[i]=s[j] then
- begin
- p:=false; break;
- end;
- if p then begin inc(ans); c[ans]:=e1[i]; end;
- end;
- for i:= to ans do q[i]:=c[i];
- qsort(,ans);
- writeln(ans);
- for i:= to ans- do write(q[i],' ');
- write(q[ans]);
- end.
【BZOJ4474】isomorphism(树的同构,哈希)的更多相关文章
- bzoj4337: BJOI2015 树的同构 树哈希判同构
题目链接 bzoj4337: BJOI2015 树的同构 题解 树哈希的一种方法 对于每各节点的哈希值为hash[x] = hash[sonk[x]] * p[k]; p为素数表 代码 #includ ...
- 【BZOJ4337】树的同构(树同构,哈希)
题意: 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如果能够把树T1T ...
- [BJOI2015]树的同构
嘟嘟嘟 判断树的同构的方法就是树上哈希. 如果树是一棵有根树,那么只要从根节点出发dfs,每一个节点的哈希值等于按传统方式算出来的子树的哈希值的结果.需要注意的是,算完子树的哈希值后要先排序再加起来, ...
- bzoj4337树的同构
树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如果能够把树T1的所有点重 ...
- BZOJ4337:[BJOI2015]树的同构(树hash)
Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...
- BZOJ4337:[BJOI2015]树的同构——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4337 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根, ...
- Andrew and Chemistry(树的同构)
Andrew and Chemistry(树的同构) 题链 将一棵树转化为最小表示法,将此时的树哈希一下,同时用map进行标记,就可以判断树是否存在同构 #include <map> #i ...
- 【PTA】浙江大学数据结构慕课 课后编程作业 03-树1 树的同构
题目内容 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右 ...
- 03-树1 树的同构 (C语言链表实现)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...
随机推荐
- ural1437
1437 记忆化 模拟倒水过程 #include <iostream> #include<cstdio> #include<cstring> #include< ...
- poj3735Training little cats
链接 构造矩阵 快速幂求解 构造矩阵a[i]为每个cati所拥有的花生总数 这里多加一维用来求和,具体是怎么求得可以看下面的一组例子 假设有3个cat a[] = {1,0,0,0} 构造单位矩阵来保 ...
- ava的动态代理机制详解
在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...
- 在windows下用python调用darknet的yolo接口
0,目标 本人计算机环境:windows7 64位,安装了vs2015专业版,python3.5.2,cygwin,opencv3.3,无gpu 希望实现用python调用yolo函数,实现物体检测. ...
- Array和ArrayList之间的区别
. Array类型的变量在声明的同时必须进行实例化(至少得初始化数组的大小),而ArrayList可以只是先声明. . Array只能存储同构的对象,而ArrayList可以存储异构的对象. 同构的对 ...
- rem手机端页面自适应布局(待修正下一篇完美布局)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (转)SpringMVC学习(十一)——SpringMVC实现Resultful服务
http://blog.csdn.net/yerenyuan_pku/article/details/72514034 Restful就是一个资源定位及资源操作的风格,不是标准也不是协议,只是一种风格 ...
- 学生管理系统之Java+Mysql
主页面: 代码如下:package appstu.view; import java.awt.BorderLayout;import java.awt.Dimension;import java.aw ...
- zabbix4.2学习笔记--TCP状态监控
Tcp的连接状态对于我们web服务器来说是至关重要的,尤其是并发量ESTAB:或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了攻击(例如SYN攻击),或是是time_wait值比 ...
- note for git
1.download https://git-for-windows.github.io/ 2.command add file to git: git add filename & git ...