『Andrew and Chemistry 树同构』
<更新提示>
<第一次更新>
<正文>
Andrew and Chemistry
Description
During the chemistry lesson Andrew learned that the saturated hydrocarbons (alkanes) enter into radical chlorination reaction. Andrew is a very curious boy, so he wondered how many different products of the reaction may be forms for a given alkane. He managed to solve the task for small molecules, but for large ones he faced some difficulties and asks you to help.
Formally, you are given a tree consisting of nn vertices, such that the degree of each vertex doesn't exceed 44 . You have to count the number of distinct non-isomorphic trees that can be obtained by adding to this tree one new vertex and one new edge, such that the graph is still the tree and the degree of each vertex doesn't exceed 44 .
Two trees are isomorphic if there exists a bijection f(v)f(v) such that vertices uu and vv are connected by an edge if and only if vertices f(v)f(v) and f(u)f(u) are connected by an edge.
Input Format
The first line of the input contains an integer nn ( 1<=n<=1000001<=n<=100000 ) — the number of vertices in the tree.
Then follow n-1n−1 lines with edges descriptions. Each edge is given by two integers u_{i}ui and v_{i}vi ( 1<=u_{i},v_{i}<=n1<=ui,vi<=n ) — indices of vertices connected by an edge. It's guaranteed that the given graph is a tree and the degree of each vertex doesn't exceed 44 .
Output Format
Print one integer — the answer to the question.
Sample Input
4
1 2
2 3
2 4
Sample Output
2
解析
题目大意:给你一个有\(n\)个点的树。当每一个点的度不超过\(4\)时这棵树是合法的。现在让你再添加一个点,在树仍然合法的情况下,一共有多少种树。当两棵树同构时视作同一种。
思路就是枚举,枚举每一个原来度数小于\(4\)的点作为加点的点,然后只要看以该点为根时这棵树是否与之前的方案同构即可。
同构就是用树哈希判,这里的方法类似于记忆化搜索:设\(f[x][fa]\)代表以\(fa\)为\(x\)的父亲时,子树\(x\)的\(hash\)值。当这个状态没有\(hash\)值的时候,我们就取其子节点的所有\(hash\)值放在一个集合里做一次映射,得到一个正整数权值,当做\(hash\)值即可。
显然这样可以保证正确性:叶节点构造的集合都是空集,其映射的正整数也是相同的,当两棵树同构的时候,对应节点也都会因为相同的集合映射出相同的\(hash\)值,那这样树的权值也就是相同的。
而记忆化搜索就帮助我们保证了效率,当然,开不下的数组可以用\(map\),因为我们知道状态总数是不超空间的:一条边只对应了一个状态。
\(Code:\)
#include <bits/stdc++.h>
using namespace std;
const int N = 100020;
int n , cnt;
vector < int > e[N];
map < int , int > f[N];
map < vector<int> , int > List;
set < int > S;
inline int dp(int x,int fa)
{
if ( f[x].count(fa) ) return f[x][fa];
vector < int > temp;
for ( int i = 0; i < e[x].size(); i++ )
{
int y = e[x][i];
if ( y == fa ) continue;
temp.push_back( dp( y , x ) );
}
sort( temp.begin() , temp.end() );
if ( !List.count( temp ) ) List[ temp ] = ++cnt;
return f[x][fa] = List[ temp ];
}
int main(void)
{
scanf("%d",&n);
for ( int i = 1; i < n; i++ )
{
int u , v;
scanf("%d%d",&u,&v);
e[u].push_back( v );
e[v].push_back( u );
}
for ( int i = 1; i <= n; i++ )
if ( e[i].size() < 4 )
S.insert( dp( i , 0 ) );
printf("%d\n",S.size());
return 0;
}
<后记>
『Andrew and Chemistry 树同构』的更多相关文章
- Andrew and Chemistry(树的同构)
Andrew and Chemistry(树的同构) 题链 将一棵树转化为最小表示法,将此时的树哈希一下,同时用map进行标记,就可以判断树是否存在同构 #include <map> #i ...
- 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry
C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...
- uva12489 Combating cancer(树同构)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud https://uva.onlinejudge.org/index.php?opt ...
- BZOJ4337: BJOI2015 树的同构(hash 树同构)
题意 题目链接 Sol 树的同构问题,直接拿hash判一下,具体流程大概是这样的: 首先转化为有根树,预处理出第\(i\)棵树以\(j\)为根时的hash值. 那么两个树同构当且仅当把两棵树的hash ...
- 『Python题库 - 填空题』151道Python笔试填空题
『Python题库 - 填空题』Python笔试填空题 part 1. Python语言概述和Python开发环境配置 part 2. Python语言基本语法元素(变量,基本数据类型, 基础运算) ...
- 『Python题库 - 简答题』 Python中的基本概念 (121道)
## 『Python题库 - 简答题』 Python中的基本概念 1. Python和Java.PHP.C.C#.C++等其他语言的对比? 2. 简述解释型和编译型编程语言? 3. 代码中要修改不可变 ...
- mongodb底层存储和索引原理——本质是文档数据库,无表设计,同时wiredTiger存储引擎支持文档级别的锁,MMAPv1引擎基于mmap,二级索引(二级是文档的存储位置信息『文件id + 文件内offset 』)
MongoDB是面向文档的数据库管理系统DBMS(显然mongodb不是oracle那样的RDBMS,而仅仅是DBMS). 想想一下MySQL中没有任何关系型数据库的表,而由JSON类型的对象组成数据 ...
- Luogu 5043 【模板】树同构([BJOI2015]树的同构)
BZOJ 4337 简单记录一种树哈希的方法:以$x$为根的子树的哈希值为$\sum_{y \in son(x)}f_y*base_i$,$f_y$表示以$y$为根的树的哈希值,其中$i$表示$f_y ...
- 『正睿OI 2019SC Day6』
动态规划 \(dp\)早就已经是经常用到的算法了,于是老师上课主要都在讲题.今天讲的主要是三类\(dp\):树形\(dp\),计数\(dp\),\(dp\)套\(dp\).其中计数\(dp\)是我很不 ...
随机推荐
- 个人项目 python实现
一. github地址:https://github.com/zjh1234562/WC 二 . PSP表格 PSP2.1 Personal Software Process Stages 预估耗时 ...
- 【转】解决Oracle 11g在用EXP导出时,空表不能导出
一.问题原因: 11G中有个新特性,当表无数据时,不分配segment,以节省空间 .insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segme ...
- JavaScript深入浅出第3课:什么是垃圾回收算法?
摘要: JS是如何回收内存的? <JavaScript深入浅出>系列: JavaScript深入浅出第1课:箭头函数中的this究竟是什么鬼? JavaScript深入浅出第2课:函数是一 ...
- mybatis-plus手记
项目源码:https://gitee.com/baomidou/mybatis-plus https://github.com/baomidou/mybatis-plus 文档:https:/ ...
- scikit-learn 中的 KMeans
语法 sklearn.cluster.KMeans(n_clusters=8, # 簇的个数, 默认为 8 init='k-means++', # 初始簇中心的获取方法 n_init=10, # 初始 ...
- Python并发编程内容回顾
Python并发编程内容回顾 并发编程小结 目录 • 一.到底什么是线程?什么是进程? • 二.Python多线程情况下: • 三.Python多进程的情况下: • 四.为什么有这把GIL锁? • 五 ...
- 什么是amp?amp有什么用处?
AMP是移动页面加速器Accelerated Mobile Pages的简称,是Google带领开发的开源项目,目的是为提升移动设备对网站的访问速度.它的核心称作AMP HTML,是一种新型的HTML ...
- 07-C#笔记-运算符
1. 支持++和-- 含义和C++中相同 2. 条件运算 同C++ 3. 位运算 ^ 异或 ~ 取反 4. 支持?:运算 5. 特殊 is 判断对象是否为某一类型. If( Ford is Car) ...
- el获取对象属性大小写问题
当el表达式首字母大写,会判断第二个字母大小写,小写,抛异常:大写,则直接拼接get,形成getter方法 当el表达式首字母小写,会判断第二个字母大小写,大写,必须第一个字母大写:否则无法获取值 e ...
- js面向对象杂谈
**万丈高楼平地起** 1. 通过命名规范创建具有私有属性的对象: 以__开头的对象为私有对象,但是实际是能访问到的. 2. 通过自执行函数中,return出来一个对象,return旁边的地方都可以写 ...