数学:UVAoj 11174 Stand in a Line

Problem J
Stand in a Line
Input: Standard Input

Output: Standard Output

 

All the people in the byteland want to stand in a line in such a way that no person stands closer to the front of the line than his father. You are given the information about the people of the byteland. You have to determine the number of ways the bytelandian people can stand in a line.

 

Input

First line of the input contains T (T<14) the number of test case. Then following lines contains T Test cases.

Each test case starts with 2 integers n (1≤n≤40000) and m (0≤m<n). n is the number of people in the byteland and m is the number of people whose father is alive. These n people are numbered 1...n.Next m line contains two integers a and b denoting that b is the father of a. Each person can have at most one father. And no person will be an ancestor of himself.

 

 

Output

For each test case the output contains a single line denoting the number of different ways the soldier can stand in a single line. The result may be too big. So always output the remainder on dividing ther the result by 1000000007.

 

Sample Input Output for Sample Input

3

3 2

2 1

3 1

3 0

3 1

2 1

 

2

6

3

 

 


Problem setter: Abdullah-al-Mahmud

Special Thanks: Derek Kisman

 

  这道题结论很有意思。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int mod=1000000007;
 6 const int maxn=40010;
 7 int cnt,fir[maxn],to[maxn],nxt[maxn];
 8 int fa[maxn],sz[maxn];
 9 long long inv[maxn];
10 
11 void addedge(int a,int b){
12     nxt[++cnt]=fir[a];to[cnt]=b;fir[a]=cnt;
13 }
14 
15 void DFS(int x){
16     sz[x]=1;
17     for(int i=fir[x];i;i=nxt[i]){
18         DFS(to[i]);
19         sz[x]+=sz[to[i]];
20     }
21 }
22 
23 void Solve(int n){
24     DFS(0);
25     long long ans=1;
26     for(int i=1;i<sz[0];i++)
27         (ans*=i)%=mod;
28     
29     for(int i=1;i<=n;i++)
30         (ans*=inv[sz[i]])%=mod;
31     
32     printf("%lld\n",ans);
33 }
34 
35 int main(){
36 #ifndef ONLINE_JUDGE
37     //freopen("","r",stdin);
38     //freopen("","w",stdout);
39 #endif
40     inv[1]=1;
41     for(int i=2;i<=40000;i++){
42         inv[i]=(1ll*inv[mod%i]*(mod-mod/i))%mod;
43         //printf("%lld\n",inv[i]);
44     }
45         
46     int T,n,m;
47     scanf("%d",&T);
48     while(T--){
49         scanf("%d%d",&n,&m);
50         memset(fir,0,sizeof(fir));
51         memset(fa,0,sizeof(fa));cnt=0;
52         for(int i=1,a,b;i<=m;i++){
53             scanf("%d%d",&a,&b);
54             fa[a]=1;addedge(b,a);
55         }
56         for(int i=1;i<=n;i++)
57             if(fa[i]==0)
58                 addedge(0,i);
59         
60         Solve(n);    
61     }
62     return 0;    
63 }

 

转载于:https://www.cnblogs.com/TenderRun/p/5570263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值