UVALive 5002 The Queue (树形Dp)
2017-03-31 13:35
387 查看
题意:
在某些特殊场合,Nadia公司为公司所有员工提供非常特别的午餐。
食物供应前,所有员工必须站在食品柜台前排队。 公司应用了排队队列的规则。 这个规则是没有人可以在他的主管面前的任何地方。 例如,如果Abul是Babul的负责人,Abul站在队列前面的第k位,那么Babul就不能站在队列前面的1和k-1之间的任何位置。
该公司拥有N名员工,每名员工只有一名主管,但没有任何主管。
您必须以可以创建队列的方式计算多少。 对于这个问题,您可以安全地假设至少以一种方式可以创建队列。
思路:要明白 同一个子树之间有位置限制,同一等级的子树之间根本没有任何牵连, 比如一个节点有10个子节点,3个子树,分别为3,3,4,那他的方案数就是 C(10,3)*dp[1]*c(7,3)*dp[2]*C(4,4)*dp[3];
代码:
在某些特殊场合,Nadia公司为公司所有员工提供非常特别的午餐。
食物供应前,所有员工必须站在食品柜台前排队。 公司应用了排队队列的规则。 这个规则是没有人可以在他的主管面前的任何地方。 例如,如果Abul是Babul的负责人,Abul站在队列前面的第k位,那么Babul就不能站在队列前面的1和k-1之间的任何位置。
该公司拥有N名员工,每名员工只有一名主管,但没有任何主管。
您必须以可以创建队列的方式计算多少。 对于这个问题,您可以安全地假设至少以一种方式可以创建队列。
思路:要明白 同一个子树之间有位置限制,同一等级的子树之间根本没有任何牵连, 比如一个节点有10个子节点,3个子树,分别为3,3,4,那他的方案数就是 C(10,3)*dp[1]*c(7,3)*dp[2]*C(4,4)*dp[3];
代码:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> using namespace std; const int Mod = 1000000007; const int maxn = 1e3 + 7; typedef long long ll; ll dp[maxn], cnt[maxn], f[maxn][maxn], book[maxn]; vector<int> v[maxn]; void init() { for(int i = 0; i <= 1000; i++) { f[i][0] = f[i][i] = 1; for(int j = 1; j < i; j++) f[i][j] = (f[i-1][j-1] + f[i-1][j])%Mod; } } void dfs(int u) { dp[u] = 1; for(int i = 0; i < v[u].size(); i++) { int to = v[u][i]; dfs(to); cnt[u] += cnt[to]; dp[u] = ((dp[u]*f[cnt[u]][cnt[to]])%Mod*dp[to])%Mod; } cnt[u]++; } int main() { init(); int t, n, ca = 1; scanf("%d", &t); while(t--) { scanf("%d", &n); int a, b, rt; memset(cnt, 0, sizeof(cnt)); memset(book, 0, sizeof(book)); for(int i = 0; i <= n; i++) v[i].clear(); for(int i = 1; i < n; i++) { scanf("%d%d", &a, &b); v[a].push_back(b); book[b] = 1; } for(int i = 1; i <= n; i++) { if(!book[i]) { rt = i; break; } } dfs(rt); printf("Case %d: %lld\n",ca++, dp[rt]); } return 0; }
相关文章推荐
- UVALive 5002/ lightoj 1382 The Queue(树形DP)
- The queue uvalive 5002 light 1382 uva 12257 (dfs加组合数学)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- Uva 6436 - The Busiest City 树形DP
- 【树形dp】UVALive 2038 Strategic game
- uvalive 3942 Remember the Word (字典树+DP)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- UVALive 3942 Remember the Word(trie + dp)
- Uva 12587 Reduce the Maintenance Cost 边双连通+树形dp+贪心
- UVALive 4015 - Caves(树形DP)
- Uva 5002 - The Queue DFS
- UVALive 3942 Remember the Word(字典树 + 简单dp)
- sgu143 树形DP Long Live the Queen
- UVALive - 2728 A Spy in the Metro DP
- sgu 143 Long live the Queen 简单树形dp
- UVALive 2038 Strategic game (树形DP,4级)
- Perfect Domination on Trees [UVALive 3346] 树形DP
- UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie)
- UvaLive 6531 Go up the ultras DP+RMQ
- UVALive 3942 Remember the Word(字典树+dp)