BZOJ 4337: BJOI2015 树的同构
来源:互联网 发布:淘宝上的黑科技 编辑:程序博客网 时间:2024/05/23 15:49
Description
树是一种很常见的数据结构。
我们把N个点,N-1条边的连通无向图称为树。
若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树。
对于两个树T1和T2,如果能够把树T1的所有点重新标号,使得树T1和树T2完全相
同,那么这两个树是同构的。也就是说,它们具有相同的形态。
现在,给你M个有根树,请你把它们按同构关系分成若干个等价类。
Input
第一行,一个整数M。
接下来M行,每行包含若干个整数,表示一个树。第一个整数N表示点数。接下来N
个整数,依次表示编号为1到N的每个点的父亲结点的编号。根节点父亲结点编号为0。
Output
输出M行,每行一个整数,表示与每个树同构的树的最小编号。
Sample Input
4
4 0 1 1 2
4 2 0 2 3
4 0 1 1 1
4 0 1 2 3
Sample Output
1
1
3
1
HINT
【样例解释】
编号为1, 2, 4 的树是同构的。编号为3 的树只与它自身同构。
100% 的数据中,1 ≤ N, M ≤ 50。
分析
树hash,越乱越爽
代码
#include <bits/stdc++.h>typedef unsigned long long ull;const ull BASE = 233333333;const int N = 55;using std::max;int read(){ int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();} while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();} return x * f;}struct Edge{ int to,next; int del;}e[N * 2];int next[N];int cnt;void add(int x,int y){ e[++cnt].to = y, e[cnt].next = next[x], next[x] = cnt, e[cnt].del = 0; e[++cnt].to = x, e[cnt].next = next[y], next[y] = cnt, e[cnt].del = 0;}int size[N],f[N];int root;int n;void get_root(int x,int fa){ size[x] = 1; f[x] = 0; for (int i = next[x]; i; i = e[i].next) { if (e[i].to == fa || e[i].del) continue; get_root(e[i].to, x); size[x] += size[e[i].to]; f[x] = max(f[x], size[e[i].to]); } f[x] = max(f[x], n - size[x]); if (!root || f[x] < f[root]) root = x;}ull a[N],tmp[N],hash[N * 2];void get_hash(int x,int fa){ for (int i = next[x]; i; i = e[i].next) { if (e[i].to != fa && !e[i].del) get_hash(e[i].to, x); } int tot = 0; for (int i = next[x]; i; i = e[i].next) { if (e[i].to != fa && !e[i].del) { tmp[++tot] = hash[e[i].to]; } } std::sort(tmp + 1, tmp + tot + 1); hash[x] = BASE; for (int i = 1; i <= tot; i++) (((hash[x] *= BASE) ^= tmp[i]) += tmp[i]) ^= tmp[i];}void init(){ for (int j = 1; j <= n + 1; j++) next[j] = 0; cnt = 1;}int main(){ int M = read(); for (int k = 1; k <= M; k++) { n = read(); init(); for (int i = 1; i <= n; i++) { int x = read(); if (x) add(x,i); } root = 0; get_root(1,0); int r1 = 0, r2 = 0; for (int i = 1; i <= n; i++) if (f[i] == f[root]) r2 = r1, r1 = i; if (r2) { for (int i = 2; i <= cnt; i+=2) if (e[i].to == r1 && e[i ^ 1].to == r2 || e[i].to == r2 && e[i ^ 1].to == r1) { e[i].del = e[i ^ 1].del = 1; break; } add(n + 1, r1), add(n + 1, r2); root = n + 1; } get_hash(root,0); a[k] = hash[root]; } for (int i = 1; i <= M; i++) for (int j = 1; j <= i; j++) if (a[i] == a[j]) { printf("%d\n",j); break; }}
阅读全文
0 0
- BZOJ 4337 BJOI2015 树的同构 Hash
- BZOJ 4337: BJOI2015 树的同构|Hash
- bzoj 4337: BJOI2015 树的同构 树哈希
- BZOJ 4337: BJOI2015 树的同构
- bzoj 4337: BJOI2015 树的同构 (树hash)
- [树hash]BZOJ 4337——BJOI2015 树的同构
- 【BZOJ】4337 BJOI2015 树的同构 树hash
- 4337: BJOI2015 树的同构
- 【BJOI2015】【BZOJ4337】树的同构
- bzoj4337 BJOI2015 树的同构
- BZOJ4337(BJOI2015)[树的同构]--树Hash
- bzoj4337: BJOI2015 树的同构 树hash
- 【bzoj4337】【BJOI2015】【树的同构】【hash】
- 【Hash】bzoj4337 BJOI2015树的同构
- [BZOJ4337 BJOI2015 树的同构]树哈希
- BZOJ4337: [BJOI2015] 树的同构(Hash)
- 【Hash】BZOJ4337(BJOI2015)[树的同构]题解
- 【树哈希】BZOJ4337 BJOI2015 树的同构
- git push 遇到missing change id的问题
- Android开发——集成友盟社会化分享遇到的坑(一)
- jquerysession.js文件
- Python程序路径,传入参数
- LightOJ
- BZOJ 4337: BJOI2015 树的同构
- eclipse上传项目到git
- 数据立方体----维度与OLAP
- 3.Activity简介
- Hadoop MapReduce学习笔记
- Neo4j 第二篇:图形数据库
- innerHTML属性
- 欢迎使用CSDN-markdown编辑器
- java 基本类型与包装类的==和equels问题