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 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    适配器设计模式初探(Java实现)
    装饰器设计模式初探及Java中实际应用举例
    主要介绍JavaEE中Maven Web 项目的结构及其它几个小问题
    基于alibaba开源的分布式数据同步系统安装配置文档otter之manager单实例配置
    基于alibaba开源的分布式数据同步系统安装配置文档otter之环境配置
    zabbix监控redis DB key的总数量
    C#操作Access数据库(创建&修改结构)
    WebDev.WebServer.exe遇到问题需要关闭
    DataSet
    ADO.NET
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5570263.html
Copyright © 2011-2022 走看看