zoukankan      html  css  js  c++  java
  • 数学: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
    ",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
    ",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 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    如何使用Remoting实现双工(转自Artech)
    解决自定义代码启动Approver Sharepoint 2010 Workflow,出现Failed on Start
    设计模式学习(目录)
    C#面向对象分析
    使用Sharepoint 2010 Client Object Model 通过SSL验证
    .NET 4.0 Location : 查看传感器状态变化
    OLAP和OLTP的 概念和区别
    组织结构及权限模型设计
    维度关系
    PHP正则替换中文让中文无处可躲
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5570263.html
Copyright © 2011-2022 走看看