lightOJ 1382 The Queue
2017-01-10 20:28
239 查看
题目分析
具体看代码吧,树形dp的思想,dp[i]表示父节点下面所有子树节点合理排列的方法数。。#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1005; const int mod = 1e9+7; #define LL long long LL head[maxn], tot, dp[maxn], num[maxn], degree[maxn], C[maxn][maxn]; struct Edge{ int to, next; }e[maxn]; void addedge(int from, int to){ e[tot].to = to; e[tot].next = head[from]; head[from] = tot++; } void dfs(int u){ dp[u] = 1, num[u] = 1; for(int i = head[u]; i != -1; i = e[i].next){ int v = e[i].to; dfs(v); num[u] += num[v]; dp[u] = C[num[u]-1][num[v]]*dp[u]%mod*dp[v]%mod; } } void init(){ tot = 0; memset(degree, 0, sizeof(degree)); memset(dp, 0, sizeof(dp)); memset(head, -1, sizeof(head)); } int main(){ for(int i = 0; i < maxn; i++){ C[i][0] = C[i][i] = 1; for(int j = 1; j < i; j++) C[i][j] = (C[i-1][j] + C[i-1][j-1])%mod; } int T, n; scanf("%d", &T); for(int kase = 1; kase <= T; kase++){ init(); scanf("%d", &n); int from, to; for(int i = 1; i < n; i++){ scanf("%d%d", &from, &to); addedge(from, to); degree[to]++; } for(int i = 1; i <= n; i++) if(!degree[i]){ dfs(i); printf("Case %d: %lld\n", kase, dp[i]); break; } } return 0; }
相关文章推荐
- UVALive 5002/ lightoj 1382 The Queue(树形DP)
- LightOJ1382 The Queue(树形DP)
- 1382 - The Queue
- [LightOJ 1151] Snakes and Ladders
- 【lightoj 1138】Trailing Zeroes (III) (二分)
- lightoj-1415:Save the Trees (单调队列优化dp)
- LightOJ-1002-Country Roads [最短路][Dijkstra]
- LightOJ-1042---Secret Origins (贪心+暴力)
- Discovering Gold LightOJ - 1030 概率DP
- LightOJ-1370 Bi-shoe and Phi-shoe
- LightOJ 1428 Melody Comparison (KMP + 后缀数组)
- 【Lightoj 1026 B Critical Links】& 桥
- 【找规律 && 快速幂 && 概率论】LightOJ - 1213 Fantasy of a Summation
- LightOj-1022-Circle in Square
- LightOJ - 1236 Pairs Forming LCM
- LightOJ 1128 Greatest Parent
- LightOJ-1245
- Halloween Costumes LightOJ - 1422 区间dp
- LightOJ 1027 Dangerous Maze【期望】
- LightOJ - 1197 Help Hanzo 较大数的区间素数筛法