ZOJ-3319
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\n"); ///原图存在环
}
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\n", ans);
}
}
return 0;
}
ZOJ-3319的更多相关文章
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- zoj 1788 Quad Trees
zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
- ZOJ 1958. Friends
题目链接: ZOJ 1958. Friends 题目简介: (1)题目中的集合由 A-Z 的大写字母组成,例如 "{ABC}" 的字符串表示 A,B,C 组成的集合. (2)用运算 ...
- ZOJ
某年浙大研究生考试的题目. 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下:1. zoj能AC:2. 若字符串形式为xzojx,则也能AC, ...
随机推荐
- BZOJ4539 [Hnoi2016]树 【倍增 + 主席树】
题目链接 BZOJ4539 题解 我们把每次复制出来的树看做一个点,那么大树实际上也就是一棵\(O(M)\)个点的树 所以我们只需求两遍树上距离: 大树上求距离,进入同一个点后在模板树上再求一次距离 ...
- AT1984 Wide Swap
AT1984 Wide Swap 题意翻译 给出一个元素集合为\(\{1,2,\dots,N\}(1\leq N\leq 500,000)\)的排列\(P\),当有\(i,j(1\leq i<j ...
- BAT大数据面试题
1.kafka的message包括哪些信息 一个Kafka的Message由一个固定长度的header和一个变长的消息体body组成 header部分由一个字节的magic(文件格式)和四个字节的CR ...
- (转)关于block使用的5点注意事项
1.在使用block前需要对block指针做判空处理. 不判空直接使用,一旦指针为空直接产生崩溃. if (!self.isOnlyNet) { if (succBlock == NULL) { // ...
- UIView的autoresizingMask属性研究
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum ...
- noip模拟赛 helloworld
分析:对于第一个点,答案为26^n - 25^n,这个很好想.另外30%的点因为n <= 5,所以可以直接暴力搜索. 数学方法不是很好处理,考虑dp,设f[i][j]为前i位匹配到危险串第j位的 ...
- 微服务学习一:idea中springboot集成mybatis
一直都想学习微服务,这段时间在琢磨这块的内容,个人之前使用eclipse,现在用intellij idea来进行微服务的开发,个人感觉intellij idea比eclipse更简洁更方便,因为int ...
- bzoj 1131 [POI2008]Sta 树形dp 转移根模板题
[POI2008]Sta Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1889 Solved: 729[Submit][Status][Discu ...
- HDU 1950 LIS(nlogn)
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- SDUT3926 kmp
bLue的二叉树 Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Problem Description Keke 是一个喜爱种树的 ...