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, ...
随机推荐
- bzoj1778: [Usaco2010 Hol]Dotp 驱逐猪猡(概率DP+高斯消元)
深夜肝题...有害身心健康QAQ 设f[i]为到达i的概率,d[i]为i的度数. 因为无限久之后炸弹爆炸的概率是1,所以最后在i点爆炸的概率实际上就是f[i]/sigma(f[]) 列出方程组 f[i ...
- python----测试04.18
# py4测试题 # 1.8 << 2 # 等于? 8转化成二进制:1000 向左移动2位: 0010 0000 转化成十进制:32 # 2.通过内置函数计算5除以2的余数 print(d ...
- Emmet缩写语法
缩写语法 Emmet 使用类似于 CSS 选择器的语法描述元素在生成的文档树中的位置及其属性. 元素 可以使用元素名(如 div 或者 p)来生成 HTML 标签.Emmet 没有预定义的有效元素名的 ...
- Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机
Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机: #user nobody; worker_processes 1; #error_log logs/error.log; #err ...
- HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- redis缓存在项目中的使用
关于redis为什么能作为缓存这个问题我们就不说了,直接来说一下redis缓存到底如何在项目中使用吧: 1.redis缓存如何在项目中配置? 1.1redis缓存单机版和集群版配置?(redis的客户 ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- C语言中两个!!的作用
两个!是为了把非0值转换成1,而0值还是0. 因为C语言中,所有非0值都表示真. 所以!非0值 = 0,而!0 = 1.所以!!非0值 = 1,而!!0 = 0.例如:i=123 !i=0 !!i=1 ...
- Linux网络监控工具nethogs
Linux网络监控工具nethogs 标签: 监控工具linux 2015-12-17 22:06 448人阅读 评论(0) 收藏 举报 分类: linux(40) 版权声明:本文为博主原创文章, ...
- echarts 使用demo
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</t ...