Distinct Subtrees

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 391    Accepted Submission(s): 190

Problem Description
Given
an unrooted tree with n nodes, the subtree is defined as a connected
component of the given tree. Two subtrees are considered the same if
there exists a map from nodes of one tree to another so that the edges
of both trees are corresponding the same pair of nodes after mapping.
  Your task is to find out how many distinct subtrees for a given unrooted tree.
 
Input
The input consists of multiple test cases. The first line of input contains an integer denoting the number of test cases.
  For each test case, the first line contains one integer n denoting the number of nodes of the given tree. (1 <= n <= 15)
  Then n-1 lines follow, each line contains two integers denoting an edge of the tree (x, y).
 
Output
For each test case, output the number of distinct subtrees for the given tree.
 
Sample Input
2
3
1 2
1 3
9
9 4
4 3
1 3
7 4
1 6
5 7
2 4
6 8
 
Sample Output
Case #1: 3
Case #2: 21
 
 
问一棵树有多少棵结构不同的子树。
做法是将它dfs形式最小表示后,插入trie中
 
#include<bits/stdc++.h>
using namespace std ;
const int N = ;
int n , g[N][N] , a[N] , b[N] , st , ans ; struct trie {
int date;
struct trie* son[];
}*root; void init() {
ans = ;
memset( g , , sizeof g ) ;
root = new trie ;
root -> date = ;
root -> son[] = NULL;
root -> son[] = NULL;
}
int insert( string s , struct trie *p ) {
struct trie *rot = p ;
for( int i = ; i < s.size() ; ++i ) {
if( rot -> son[ s[i] - ''] == NULL ) {
trie *t = new trie ;
t -> date = ;
t -> son[] = NULL ;
t -> son[] = NULL ;
rot -> son[ s[i] - '' ] = t ;
}
rot = rot -> son[ s[i] - '' ] ;
}
if( rot -> date == ) return ;
rot -> date = ;
return ;
} string dfs1( int u , int p ) {
string vs = "";
vector<string>q;
for( int i = ; i < n ; ++i )
if( (st&(<<i)) && g[u][i] && i != p )
q.push_back(dfs1(i,u));
sort( q.begin() , q.end() );
for( int i = ; i < q.size() ; ++i ) vs += q[i] ;
vs += "";
return vs ;
} int solve() {
int f = , t ;
string s ;
for( int i = ; i < n ; ++i ) if( st&(<<i) ){
s = dfs1( i , - );
t = insert( s , root );
if( (!t)&& (!f) ) return ;
f = ;
}
return ;
} int main() {
string s ;
ios::sync_with_stdio();
int _ , cas = ; cin >> _ ;
while( _-- ) {
cin >> n ;
init() ;
for( int i = ; i < n ; ++i ) {
int u , v ; cin >> u >> v ;
u-- , v-- ;
g[u][v] = g[v][u] = ;
}
for( int i = ; i < (<<n) ; ++i ) {
st = i ;
ans += solve();
}
cout << "Case #"<< cas++ << ": " << ans << endl ;
}
return ;
}

HDU 4013 Distinct Subtrees(树的最小表示)的更多相关文章

  1. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  2. HDU 1954 Subway tree systems (树的最小表示法)

    题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构. 分析:同构的树经过最小表示会转化成两个相等的串. 方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即 ...

  3. hdu 6301 Distinct Values (思维+set)

    hdu 6301 Distinct Values 题目传送门 题意: 给你m个区间,让你求出一个长度为n的区间且满足在这些区间的数不重复, 并且要求字典序最小 思路: 如果我们已经求出这个序列了,你会 ...

  4. POJ 1635 树的最小表示法/HASH

    题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...

  5. HDU 5224 Tom and paper(最小周长)

    HDU 5224 Tom and paper(最小周长) Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  6. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  7. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)

    统计树的最小层数以及位于该层数上的叶子节点个数即可. 代码里建树我用了邻接链表的存储方式——链式前向星,不了解的可以参考,非常好用: http://www.cnblogs.com/chenxiwenr ...

随机推荐

  1. Java语言Lang包下常用的工具类介绍_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 无论你在开发哪中 Java 应用程序,都免不了要写很多工具类/工具函数.你可知道,有很多现成的工具类可用,并且代码质量都 ...

  2. 【NOIP2012模拟8.20】Memory

    题目 话说某一天,CD在爬树的时候发现了树干上有一大串奇怪的符文,于是好奇的CD就从头到尾看了一遍.看完一遍以后CD觉得,最后一段文字好像很眼熟,好像在前面见过.对于CD来说,一开始看到的符文会印象特 ...

  3. 使用 Markdown 写博客

    后台设置(左侧边栏区找到-设置默认编辑器). 设置为 Markdown 后保存,即可在编辑新博客时生效.

  4. [CSP-S模拟测试]:木板(数学)

    题目传送门(内部题68) 输入格式 输入有若干行,每行一个整数$N$,以$0$结束 输出格式 每行一个整数表示方案数,方案不同当且仅当$E$.$F$.$G$的坐标不同 样例 样例输入: 1020100 ...

  5. 20175214 《Java程序设计》第11周学习总结

    20175214 <Java程序设计>第11周学习总结 本周学习任务总结 1.根据<java2实用教程>和蓝墨云学习视频学习第十三章: 2.尝试将课本重点内容用自己的话复述手打 ...

  6. SQL Server函数大全(三)----Union与Union All的区别

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并在一起显示出来. union和unio ...

  7. 微信小程序 button 组件

    button 组件 拥有强大的功能 自身可以拥有很多跟微信风格的样式,且是 表单 和 开放的能力 重要的 按钮 button 的属性: size: 类型 字符串 按钮的大小 属性值:default 默 ...

  8. Ubuntu 系统搭建LNMP环境

    当前Linux版本:Ubuntu16.04 一.安装Nginx 在终端中输入命令 " sudo apt-get install nginx ",在确认安装完成后,在浏览器中访问 l ...

  9. 微信企业号 发送信息 shell

    微信企业号发送信息shell #可作为shell函数模块调用,用于微信通知.jenkins发版微信通知等等 # 微信API官方文档 https://work.weixin.qq.com/api/doc ...

  10. node+express POST请求

    // POST 登录 app.post('/login', function (req, res) { // 定义了一个post变量,用于暂存请求体的信息 let [post,addSql,addSq ...