『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\)是我很不 ...
随机推荐
- elasticsearch regexp查询特殊字符处理
regexp表面意思就是正则查询,但是如果遇到,查询条件中包含特殊的字符串, 就会发现,需要进行相应的转义处理 需要处理Lucene regexps即可: /** * 转义字符串中的特殊字符 * 仅过 ...
- Mybatis 子查询
在查询数据库时,需要以查询结果为查询条件进行关联查询. 在mybatis中通过association标签和collection标签实现子查询. 1. collection(集合)和associatio ...
- C# HttpClient Post 参数同时上传文件 上传图片 调用接口
// 调用接口上传文件 using (var client = new HttpClient()) { using (var multipartFormDataContent = new Multip ...
- 用 Splashtop Wired XDisplay HD 让 ipad做电脑扩展屏幕__亲测有效
参考: [1]https://blog.csdn.net/Tang_Chuanlin/article/details/86433152
- LCD编程_使用调色板
在前面的博客中,使用的像素格式都是16bpp,24bpp(24bpp实际实际上就是32bpp)?如果想使用8bpp时,就需要使用调色板. 在以前的博客中,曾经说过,在framebuffer中如果每个像 ...
- Android Studio + uiautomator 配置运行
1.在build.gradle中添加依赖: androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v1 ...
- index获取子DOM对象在父DOM对象的内位置索引值
<script type="text/javascript"> $(function(){ var $p1=$('#id1 p:visible'); ...
- VIJOS-P1320 清点人数
JDOJ 1427: VIJOS-P1320 清点人数 题目传送门 Description 初始时,火车上没有学生:当同学们开始上火车时,年级主任从第一节车厢出发走到最后一节车厢,每节车厢随时都有可能 ...
- 小学四则运算口算练习app---No.4
今天主要是改了出题页中各个组件的位置以及时间的接收还有时间控制,代码如下:(但是存在一个问题 设置页面点击确定按钮进入出题界面时有时会闪退,未解决!) CalculatorActivity.clas ...
- Hibernate框架学习1
框架是什么 1.框架是用来提高开发效率的 2.封装了好了一些功能.我们需要使用这些功能时,调用即可.不需要再手动实现. 3.所以框架可以理解成是一个半成品的项目.只要懂得如何驾驭这些功能即可. orm ...