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. css3 宽度百分比减去固定宽度 无效问题

    一定要注意中间横线的间距才有效果 正确 width: calc(50% - 10px); 错误 width:calc(50%-10px);

  2. linux运维、架构之路-CentOS6.9安装Zabbix3.4.1

    一.LAMP环境安装 1.环境 [root@m01 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@m01 ~]# uname ...

  3. php中如何上传整个文件夹里的所有文件?

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  4. Oralce动态的创建按月的分区

    说明: XXX为一个配置表,里面配置了要分区的表明,即CODENO = 'PARTITIONTABLENAME',只有每个月月底的时候,才会进入IF的判断,此外还有一个定时任务,每天去执行即可. 存储 ...

  5. 【bzoj3564】 [SHOI2014]信号增幅仪

    题目描述: 无线网络基站在理想状况下有效信号覆盖范围是个圆形.而无线基站的功耗与圆的半径的平方成正比. 现给出平面上若干网络用户的位置,请你选择一个合适的位置建设无线基站.... 就在你拿起键盘准备开 ...

  6. android 开发,视频群聊引发短信异常

    说到 NDK 开发,其实是为了有些时候为了项目需求需要调用底层的一些 C/C++ 的一些东西:另外就是为了效率更加高些. 但是很多时候能不用就不用:这个是啥原因?个人感觉有些时候是觉得麻烦,首先要配置 ...

  7. InfluxDB安装使用

    influxdb简介   启动步骤 服务启停:sudo service influxdb start/stop/restart 安装过程: 1.增加yum源 cat <<EOF | sud ...

  8. Wowza 4 vod 录播多层目录无法播放问题

    找到一个解决方案,但是无法下载zip包 https://stackoverflow.com/questions/21303361/how-to-stream-mp4-files-from-subdir ...

  9. Ajax的封装。

    封装 Ajax 因为Ajax 使用起来比较麻烦,主要就是参数问题,比如到底使用GET 还是POST:到 底是使用同步还是异步等等,我们需要封装一个Ajax 函数,来方便我们调用.    封装支持接收来 ...

  10. (二)Maven之坐标和依赖

    目录 坐标 依赖 目录 坐标 引言: 坐标是依赖管理的基础,是构建的唯一标识. 组成元素: 使用groupId.artifactId.version.packaging.classifier标签即可定 ...