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, ...
随机推荐
- BZOJ3781:小B的询问——题解
https://www.luogu.org/problemnew/show/2709 http://www.lydsy.com/JudgeOnline/problem.php?id=3781 题目描述 ...
- POJ 2774 求两个串的最长公共前缀 | 后缀数组
#include<cstdio> #include<algorithm> #include<cstring> #define N 200005 using name ...
- 怎么使用formBuilder以拖拽方式编写页面
1.以admin用户登录系统,打开formbuilder http://localhost:8081/framework/main/formbuilder 2.从右方组件中,用鼠标拖拽页面所需的组件到 ...
- 解决 cmd dos 下 无法显示中文
在做程序开发的时候经常需要在使用命令行进行操作, dos环境本身是不支持中文的,有时候中文编码的问题就像苍蝇一样讨厌,下面提供几种常用的手段解决win7环境下中文显示乱码的问题: 方法一: 修改注册表 ...
- MYSQL性能察看
http://fengbin2005.iteye.com/blog/1580214 网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步 ...
- RabbitMQ基础概念(消息、队列、交换机)
1.消息的确认 RabbitMQ需要对每一条发送的消息进行确认.消费者必须通过AMQP的basic.ack命令显式地向RabbitMQ发送一个确认,或者在订阅到队列的时候就将auto_ack参数设置为 ...
- [技巧篇]10.那些年我们一起优化过的MyEclipse8.6
这里里面是针对于四海的给位学生预留的视频,请自行下载,我希望对大家有所帮助 我这里使用了百度网盘,这里的东西还是比较多的!如果喜欢请关注我,当好胖先生的粉丝 链接:http://pan.baidu.c ...
- Inner join case when
SELECT ( ), wn.ActualWorkflowNumber) + ' ' + wi.SN ) AS SN , wi.RecordID , wi.WorkflowName , wc.Work ...
- UVA 1648 Business Center
https://vjudge.net/problem/UVA-1648 设上升x层,列个方程解出来,再把x带回去 #include<cmath> #include<cstdio> ...
- spoj COT2 - Count on a tree II
COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...