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 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    一些常用的 Git 进阶知识与技巧
    自动充值平台开发进程之联通卡密一最终实现
    自动充值平台开发进程之联通卡密一准备阶段
    自动充值平台开发进程之联通卡密一显示界面
    [注入] 突破 SESSION 0 隔离的远线程注入
    git 撤销 push 提交
    [C#] 获取时间戳(秒/毫秒)
    html判断浏览器是否支持,不支持则输出
    [C#] MD5 加密
    [Windows驱动开发] 进程的pid handle eprocess之间相互转换的方法
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5570263.html
Copyright © 2011-2022 走看看