<更新提示>

<第一次更新>


<正文>

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 树同构』的更多相关文章

  1. Andrew and Chemistry(树的同构)

    Andrew and Chemistry(树的同构) 题链 将一棵树转化为最小表示法,将此时的树哈希一下,同时用map进行标记,就可以判断树是否存在同构 #include <map> #i ...

  2. 【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 ...

  3. uva12489 Combating cancer(树同构)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud https://uva.onlinejudge.org/index.php?opt ...

  4. BZOJ4337: BJOI2015 树的同构(hash 树同构)

    题意 题目链接 Sol 树的同构问题,直接拿hash判一下,具体流程大概是这样的: 首先转化为有根树,预处理出第\(i\)棵树以\(j\)为根时的hash值. 那么两个树同构当且仅当把两棵树的hash ...

  5. 『Python题库 - 填空题』151道Python笔试填空题

    『Python题库 - 填空题』Python笔试填空题 part 1. Python语言概述和Python开发环境配置 part 2. Python语言基本语法元素(变量,基本数据类型, 基础运算) ...

  6. 『Python题库 - 简答题』 Python中的基本概念 (121道)

    ## 『Python题库 - 简答题』 Python中的基本概念 1. Python和Java.PHP.C.C#.C++等其他语言的对比? 2. 简述解释型和编译型编程语言? 3. 代码中要修改不可变 ...

  7. mongodb底层存储和索引原理——本质是文档数据库,无表设计,同时wiredTiger存储引擎支持文档级别的锁,MMAPv1引擎基于mmap,二级索引(二级是文档的存储位置信息『文件id + 文件内offset 』)

    MongoDB是面向文档的数据库管理系统DBMS(显然mongodb不是oracle那样的RDBMS,而仅仅是DBMS). 想想一下MySQL中没有任何关系型数据库的表,而由JSON类型的对象组成数据 ...

  8. Luogu 5043 【模板】树同构([BJOI2015]树的同构)

    BZOJ 4337 简单记录一种树哈希的方法:以$x$为根的子树的哈希值为$\sum_{y \in son(x)}f_y*base_i$,$f_y$表示以$y$为根的树的哈希值,其中$i$表示$f_y ...

  9. 『正睿OI 2019SC Day6』

    动态规划 \(dp\)早就已经是经常用到的算法了,于是老师上课主要都在讲题.今天讲的主要是三类\(dp\):树形\(dp\),计数\(dp\),\(dp\)套\(dp\).其中计数\(dp\)是我很不 ...

随机推荐

  1. SAP CRM Product Interlinkage - Customer Product ID的一个例子

    For detail technical introduction about relationship, please refer to this wiki. The relationship tr ...

  2. 第二篇Scrum冲刺博客

    第二篇Scrum冲刺博客 一.站立式会议 提供当天站立式会议照片一张 二.每个人的工作 成员 已完成工作 明天计划完成的工作 遇到的困难 林剑峰 初步学习小程序的编写.博客园的撰写 初步完成用户界面 ...

  3. 阿里云查看本服务器 公网ip地址 命令

    阿里云的服务器用命令ifconfig查看的是本机内网地址 那如何访问公网地址呢? curl httpbin.org/ip

  4. (原)堆叠hourglass网络

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11486185.html 论文: https://arxiv.org/abs/1603.06937 官 ...

  5. k8s dashboard 解决secret自建证书导致浏览器访问限制

    解决参考: https://www.jianshu.com/p/c6d560d12d50   熟悉dashboard yaml文件所创建的资源 wget https://raw.githubuserc ...

  6. Jmeter 在 beanshell 脚本中写日志

    JMETER 在执行时,会写日志数据,我们在编写脚本的时候也可以自己写日志. 日志记录再jmeter 的bin 目录的 jmeter.log 文件中. jmeter 比较人性化,它在这里提供了脚本可以 ...

  7. DF1协议常用命令

    PCCC:Programmable Controller Communication Commands. AB PLC常用指令 根据http://www.iatips.com/pccc_tips.ht ...

  8. construct2 入门

    construct2特点 construct2是一款跨平台二维游戏开发引擎,不需要编码,通过定义各个部件和事件完成html5的游戏开发.该引擎可以将开发的封装成多种形式,如phonegap.cocoo ...

  9. linux突然不能上网,eth0网卡消失

    情况:之前可以正常浏览网页,没有动其它的地方,浏览器突然不能上网 ifconfig # 发现eth0网卡不见了,只有lo卡 ifconfig -a # 发现了eth0,但是没有IP地址 dhclien ...

  10. Win10开启快速启动后关机变重启

    同样可以用来解决,“msconfig引导为空”,“默认操作系统为空”,“win10改为uefi启动后关机变重启”,“legacy转uefi后无法关机” 问题起因 换完主板和cpu后,新的主板在开机时总 ...