zoukankan      html  css  js  c++  java
  • ZOJ-3319

    Islands

    Time Limit: 1 Second      Memory Limit: 32768 KB

    There are N islands and some directed paths between some of them. You are the transportation minister of these islands, and you are going to build some more directed paths so that every island belongs to exactly one cycle. A cycle is two or more islands I1, I2, I3, ... Ik, such that there are paths: I1 -> I2, I2 -> I3, ... and Ik -> I1. Besides the cycles, there should not be any extra edges. Of course, you cannot build a path from an island to itself. You want to calculate how many different ways you can build paths to satisfy the restriction.

    Input

    There are multiple cases (no more than 100). For each case, the first line is an integer N (1 <= N <= 100), giving the number of islands. N = 0 indicates the end of input. Then N lines follow, each with N characters, giving the paths between islands. The j-th character of the i-th line is either 'Y' or 'N'. 'Y' means there is a path from the i-th island to the j-th island, while 'N' means there is no path from the i-th island to the j-th island. The i-th character of the i-th line is always 'N'.

    Output

    For each case, you should output how many different ways you can build paths to satisfy the restriction. The answer may be very large, so just output the answer MOD 10,000,007.

    Sample Input

    2
    NN
    NN
    2
    NY
    YN
    3
    NNN
    NNN
    NNN
    3
    NYY
    NNN
    NNN
    0
    

    Sample Output

    1
    1
    2
    0
    

    Author: HANG, Hang
    Source: The 10th Zhejiang University Programming Contest

    /**
              题意:给出一个有向图,然后让所有点在一个环内;
              做法:组合数,错排
                        错排,当加一个点在链中时,可以将链看成是一个点,然后进行排列的方法有多少种
                        组合数,链可以是一条链加点成环,也可以时多条链加点成环。
    **/
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <set>
    #include <vector>
    #define mod 10000007
    #define maxn 110
    using namespace std;
    char ch[maxn][maxn];
    int indegree[maxn];
    int outdegree[maxn];
    int cuo[maxn];
    long long  C[maxn][maxn];
    int n;
    void  init()
    {
        C[0][0] = 1;
        for(int i = 1 ; i <= 100 ; i++)
        {
            C[i][0] = C[i][i] = 1;
            for(int j = 1; j < i; j++)
            {
                C[i][j] = (C[i-1][j] + C[i-1][j-1] )%mod;
            }
        }
        cuo[0] = 1;
        cuo[1] = 0;
        for(int i=2; i<=100; i++)  ///错排
        {
            cuo[i] = ((i-1) *(cuo[i-1] + cuo[i-2]))%mod;
        }
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
        init();
        while(~scanf("%d",&n))
        {
            if(n == 0) break;
            memset(indegree,0,sizeof(indegree));
            memset(outdegree,0,sizeof(outdegree));
            for(int i=0; i<n; i++)
            {
                scanf("%s",ch[i]);
                for(int j=0; j<n; j++)
                {
                    if(ch[i][j] == 'Y')
                    {
                        indegree[j] ++;
                        outdegree[i]++;
                    }
                }
            }
            bool prime = true;
            for(int i=0; i<n; i++)
            {
                if(indegree[i] > 1 || outdegree[i] >1)
                {
                    prime = false;
                    break;
                }
            }
            if(!prime)
            {
                printf("0
    ");      ///原图存在环
            }
            else
            {
                int In= 0,Out = 0,tot = 0;
                for(int i=0; i<n; i++)
                {
                    if(indegree[i] == 0)
                    {
                        In++;
                        if(outdegree[i] == 0)   
                        {
                            Out++;              ///孤立的点的个数
                        }
                    }
                }
                tot = In - Out;    ///弧的个数
                long long ans = 0;
                for (int i = 0; i <= tot; i++)
                {
                    ans = (ans + C[tot][i]*cuo[i+Out])%mod;
                }
                printf("%lld
    ", ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    随笔-CompletableFuture的使用
    什么是spring,它能够做什么?
    springboot使用mail提示没有该类型的bean
    js中对对象经行判空
    记录一下第一次webSocket通信成功
    RuoYi-Cloud从配置到运行
    Git配置环境变量
    idea反编译失败 /* compiled code */的解决方法
    MacOS终端ssh登陆红旗系统后中文乱码问题解决
    佛教中的“荤腥”(辛腥)指的是什么?
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4424110.html
Copyright © 2011-2022 走看看