HDU 4013 Distinct Subtrees(树的最小表示)
Distinct Subtrees
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 391 Accepted Submission(s): 190
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.
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).
#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(树的最小表示)的更多相关文章
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- HDU 1954 Subway tree systems (树的最小表示法)
题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构. 分析:同构的树经过最小表示会转化成两个相等的串. 方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即 ...
- hdu 6301 Distinct Values (思维+set)
hdu 6301 Distinct Values 题目传送门 题意: 给你m个区间,让你求出一个长度为n的区间且满足在这些区间的数不重复, 并且要求字典序最小 思路: 如果我们已经求出这个序列了,你会 ...
- POJ 1635 树的最小表示法/HASH
题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...
- HDU 5224 Tom and paper(最小周长)
HDU 5224 Tom and paper(最小周长) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d &a ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)
统计树的最小层数以及位于该层数上的叶子节点个数即可. 代码里建树我用了邻接链表的存储方式——链式前向星,不了解的可以参考,非常好用: http://www.cnblogs.com/chenxiwenr ...
随机推荐
- 【LuoguP3348】[ZJOI2016]大森林
题目链接 题目描述 小Y家里有一个大森林,里面有n棵树,编号从1到n.一开始这些树都只是树苗,只有一个节点,标号为1.这些树都有一个特殊的节点,我们称之为生长节点,这些节点有生长出子节点的能力. 小Y ...
- Ubuntu 压缩文件命令
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file -c: Create an archive. -z: Compress the ...
- 跳转控制语句continue
1 continue的使用场景: 1.1 在循环语句中 注意:离开使用场景的存在是没有意义的 2 continue的作用: 2.1 单层循环对比break,然后总结两者的区别 2.1.1 break ...
- 拆系数$FFT$($4$遍$DFT$)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> ...
- [CSP-S模拟测试]:小P的生成树(数学+Kruskal)
题目描述 小$P$是个勤于思考的好孩子,自从学习了最大生成树后,他就一直在想:能否将边权范围从实数推广到复数呢?可是马上小$P$就发现了问题,复数之间的大小关系并没有定义.于是对于任意两个复数$z_1 ...
- [CSP-S模拟测试]:e(树上主席树)
题目传送门(内部题66) 输入格式 第一行,一个正整数$n$,一个自然数$q$,一个整数$type$.第二行,$n$个正整数,代表$a_i$.接下来$n-1$行,每行两个正整数$u$.$v$,代表树中 ...
- 文档流&文字&CSS常用命令
文档流 文档流就是文档内元素流动方向 流动方向 内联元素从左往右流,宽度不够,之字形,且元素会被截断 块元素从上往下流动,一排一排 注意事项 内联元素中有英文单词,流动时宽度不够,英文单词会整体迁移, ...
- anaconda 下载包速度慢
更新清华镜像资源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda ...
- p2619 [国家集训队2]Tree I [wqs二分学习]
分析 https://www.cnblogs.com/CreeperLKF/p/9045491.html 反正这个博客看起来很nb就对了 但是不知道他在说啥 实际上wqs二分就是原来的值dp[x]表示 ...
- vb语法
If IsNull(rs(0)) = False Then picPath = rs(0) End If 判断查询数据库结果集是否为null, 如果用rs(0)则会报错,这样获取不到这条记录的null ...