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. Spring Boot 整合Spring Data JPA

    Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...

  2. LeetCode--152--乘积最大子序列(python)

    给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6.示例 2: 输 ...

  3. word的公式编辑器在插入对象里面!!!!!!!!!!!!!

    word的公式编辑器在  插入->对象  里面!!!!!!!!!!!!!

  4. JDK,JRE与JVM浅析

    JAVA的两个特性: 1, 开源-指的是源代码免费 2,跨平台(可移植性好) 跨平台:是指跨操作系统 JVM(java virtual machine,java虚拟机) JVM就像是两国谈判时的使者充 ...

  5. [CF342C]Cupboard and Balloons 题解

    前言 博主太弱了 题解 这道题目是一个简单的贪心. 首先毋庸置疑,柜子的下半部分是要放满的. 于是我们很容易想到,分以下三种情况考虑: \[\small\text{请不要盗图,如需使用联系博主}\] ...

  6. linux/Unix下的vim/vi指令的使用方法

    概述 以下这篇文章介绍的是关于vim的使用方法,由于我本身对linux没有太多的研究,写下的这篇文章纯属是在实际中经常使用vim指令,想通过这篇文章记录下来,方便以后使用时查找方便.个人认为,对于普通 ...

  7. BootStrap 用法

    1 下载bootstrap组件 2  在jsp页面中加入bootstrap <link rel="stylesheet" type="text/css" ...

  8. 大数据笔记(十二)——使用MRUnit进行单元测试

    package demo.wc; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.io.IntW ...

  9. Linux内核调试方法总结之Jprobes

    Jprobes [用途] 类似于Kprobes和Return Probes,区别在于,Kprobes可以在任意指令处插入探针,Jprobes只在函数入口插入探针,而Return Probes则是在函数 ...

  10. SynchronousQueue 源码分析

    SynchronousQueue SynchronousQueue 能解决什么问题?什么时候使用 SynchronousQueue? 1)SynchronousQueue 没有任何内部容量. 2)Sy ...