UVA 11174 Stand in a Line 树dp+算
主题链接:点击打开链接
题意:白书的P103.
加个虚根就能够了。。。然后就是一个多重集排列。
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner; public class Main {
static int N = 40100;
ArrayList<Integer>[] G = new ArrayList[N];
static long mod = 1000000007;
long[] way = new long[N];
long[] fac = new long[N];
int n, m;
int[] father = new int[N], siz = new int[N];
long Pow(long x, long y){
long ans = 1;
while(y > 0){
if(y%2 == 1L)
ans = (ans*x)%mod;
x = (x*x)%mod;
y >>= 1;
}
return ans;
}
void dfs(int u, int fa){
siz[u] = 1; way[u] = 1L;
for(int i = 0; i < G[u].size(); i++){
int v = G[u].get(i); if(v == fa)continue;
dfs(v, u);
siz[u] += siz[v];
way[u] = (way[u]*way[v])%mod;
}
way[u] = (way[u]* fac[siz[u]-1]) %mod;
for(int i = 0; i < G[u].size(); i++){
int v = G[u].get(i); if(v == fa)continue;
way[u] = (way[u] * Pow(fac[siz[v]], mod-2))%mod;
}
}
void init(){
n = cin.nextInt(); m = cin.nextInt();
for(int i = 0; i <= n; i++){
father[i] = 0;
G[i].clear();
}
while(m-- > 0){
int u = cin.nextInt(), v = cin.nextInt();
G[v].add(u);
father[u] = v;
}
for(int i = 1; i <= n; i++)
if(father[i] == 0){
G[0].add(i);
}
}
void work() {
for(int i = 0; i < N; i++)G[i] = new ArrayList();
fac[0] = 1L;
for(int i = 1; i < N; i++) fac[i] = fac[i-1]*i%mod;
int T = cin.nextInt();
while(T-->0){
init();
dfs(0,0);
out.println(way[0]);
}
} Main() {
cin = new Scanner(System.in);
out = new PrintWriter(System.out);
} public static void main(String[] args) {
Main e = new Main();
e.work();
out.close();
} public Scanner cin;
public static PrintWriter out;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
UVA 11174 Stand in a Line 树dp+算的更多相关文章
- uva 11174 Stand in a Line
// uva 11174 Stand in a Line // // 题目大意: // // 村子有n个村民,有多少种方法,使村民排成一条线 // 使得没有人站在他父亲的前面. // // 解题思路: ...
- UVA 11174 Stand in a Line 树上计数
UVA 11174 考虑每个人(t)的所有子女,在全排列中,t可以和他的任意子女交换位置构成新的排列,所以全排列n!/所有人的子女数连乘 即是答案 当然由于有MOD 要求逆. #include & ...
- uva 11174 Stand in a Line (排列组合)
UVa Online Judge 训练指南的题目. 题意是,给出n个人,以及一些关系,要求对这n个人构成一个排列,其中父亲必须排在儿子的前面.问一共有多少种方式. 做法是,对于每一个父节点,将它的儿子 ...
- UVA 11174 Stand in a Line (组合+除法的求模)
题意:村子里有n个人,给出父亲和儿子的关系,有多少种方式可以把他们排成一列,使得没人会排在他父亲的前面 思路:设f[i]表示以i为根的子树有f[i]种排法,节点i的各个子树的根节点,即它的儿子为c1, ...
- 【递推】【推导】【乘法逆元】UVA - 11174 - Stand in a Line
http://blog.csdn.net/u011915301/article/details/43883039 依旧是<训练指南>上的一道例题.书上讲的比较抽象,下面就把解法具体一下.因 ...
- UVA 11174 Stand in a Line,UVA 1436 Counting heaps —— (组合数的好题)
这两个题的模型是有n个人,有若干的关系表示谁是谁的父亲,让他们进行排队,且父亲必须排在儿子前面(不一定相邻).求排列数. 我们假设s[i]是i这个节点,他们一家子的总个数(或者换句话说,等于他的子孙数 ...
- 数学:UVAoj 11174 Stand in a Line
Problem J Stand in a Line Input: Standard Input Output: Standard Output All the people in the bytela ...
- uva 12452 Plants vs. Zombies HD SP (树DP)
Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a ...
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
随机推荐
- hdu 4464 水
http://acm.hdu.edu.cn/showproblem.php?pid=4464 现场赛总会有水题,这就是最水的一道,预计也就是能当高校的上机题,保研用,呵呵~~~ #include &l ...
- Chromium
Chromium多进程架构 多进程架构 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//Start_Here_Bac ...
- Axuer 网页
http://www.webppd.com/axure/
- UVA 674 (入门DP, 14.07.09)
Coin Change Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...
- Android 内存泄漏优化汇总
android内存泄漏优化摘要 博客分类: android android内存溢出OutOfMemoryError . android移动应用程序的内存分配一般是8凯瑟琳约,不正确地假定处理内存处理非 ...
- Entity Framework mvc Code First data migration
1. Code First 可以先在代码里写好数据模型,自动生成DB.下一次启动的时候会根据__MigrationHistory判断 数据库是否和模型一致. 详情参考:http://blogs.msd ...
- MVC5 Entity Framework学习参加排序、筛选和排序功能
上一篇文章实现Student 基本的实体CRUD操作.本文将展示如何Students Index页添加排序.筛选和分页功能. 以下是排序完成时.经过筛选和分页功能截图,您可以在列标题点击排序. 1.为 ...
- HDU-2857-Mirror and Light(计算几何)
Problem Description The light travels in a straight line and always goes in the minimal path between ...
- 解决PhpCms V9后台无法上传图片
PHPCMS V9 在近期一次更新的版本号(9.4.2)中因为代码推断失误.导致PHPCMS在后台更新文章无法上传图片而导致的bug.在PHPCMS论坛中找到了暂时解决方式,希望PHPCMS官方能尽快 ...
- android数据储存之应用安装位置
原文地址:http://developer.android.com/guide/topics/data/install-location.html#Compatiblity 从API8開始,你能够将你 ...