• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

HDU 1294 Rooted Trees Problem

 

 

原来是C(n,m)越界了- -难怪一直不出答案- -#

Rooted Trees Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 472    Accepted Submission(s): 161


Problem Description
Give you two definitions tree and rooted tree. An undirected connected graph without cycles is called a tree. A tree is called rooted if it has a distinguished vertex r called the root. Your task is to make a program to calculate the number of rooted trees with n vertices denoted as Tn. The case n=5 is shown in Fig. 1.
 

 

Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer means n(1<=n<=40) .
 

 

Output
For each test case, there is only one integer means Tn.
 

 

Sample Input
1
2
5
 

 

Sample Output
1
1
9
 

 

Author
SmallBeer(CML)
 

 

Source
杭电ACM集训队训练赛(VIII)
 

 

Recommend
lcy
 
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <map>
 3 #include <queue>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 using namespace std;
11 #define maxn 105
12 #define mod 1000000007
13 #define ll long long
14 #define INF 0x7fffffff
15 int n, m, s;
16 ll f[maxn];
17 int cnt[maxn];
18 ll gcd(ll a, ll b){return b ? gcd(b, a%b) : a;}
19 ll C(ll n, ll m)
20 {
21     m = min(m, n - m);
22     ll mul = 1, div = 1;
23     for (int i = 0; i < m; i++)
24     {
25         mul *= (n - i);
26         div *= (i + 1);
27         ll g = gcd(mul, div);
28         mul /= g, div /= g;
29     }
30     return mul / div;
31 }
32 void dfs(int n,int a,int step,ll t){
33     if (t>n)return;
34     if (t == n){
35         ll s = 1;
36         int k = 1;
37         for (int i = 1; i < step; i++){
38             if (cnt[i] == 0)continue;
39             if (cnt[i] != cnt[i - 1]){
40                 s *= C(f[cnt[i-1]] + k - 1, k);
41                 k = 0;
42             }
43             k++;
44         }
45         s *= C(f[cnt[step-1]] + k - 1, k);
46         f[n+1] += s;
47         return;
48     }
49     for (int i = a; i <= n; i++){
50         cnt[step] = i;
51         dfs(n, i, step + 1, t + i);
52     }
53 }
54 int main(){
55     int cas = 1;
56     int t;
57     /*scanf("%d", &t);
58     while (t--){
59         scanf("%d", &n));
60     }*/
61     f[1] = f[2] = 1;
62     for (int i = 3; i <= 40; i++){ f[i] = 0; dfs(i - 1, 1, 0, 0); }
63     while (~scanf("%d", &n)){
64         printf("%I64d\n", f[n]);
65     }
66     return 0;
67 }
View Code 2013-10-25 12:05:49 
posted @ 2013-10-24 23:42  HaibaraAi  阅读(182)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3