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, ...
随机推荐
- cdh版本的sqoop安装以及配置
sqoop安装需要提前安装好sqoop依赖:hadoop .hive.hbase.zookeeper hadoop安装步骤请访问:http://www.cnblogs.com/xningge/arti ...
- Python 编程实战提高测试工作效率实例之svn 文件管理
#coding=utf-8 ''' Created on 2016年8月22日 @author:Tom Gao ''' importre importos importtime "" ...
- sass的循环for,while,each
1. for @for $i from 1 to 10 { .border-#{$i} { border: #{$i}px solid blue; } } 2. while $i: 6; @while ...
- 「LibreOJ NOIP Round #1」七曜圣贤
题目啰嗦:支持三个操作: 不可重复集合:1.加入一个数 2.删除一个数 3.恢复目前最早的一次删除的数 操作可能不合法,每次有效操作之后求集合的mex(最小没有出现过的数) 50组数据+1e6,必须O ...
- setTimeout()的应用
错误写法:setTimeout(window.close(),5000); 正确写法:setTimeout(window.close,5000); 或者 setTimeout(function(){ ...
- main函数的传参与返回
1.谁给main函数传参(1)调用main函数所在的程序的它的父进程给main函数传参,并且接收main的返回值.2.为什么需要给main函数传参(1)首先,main函数不传参是可以的,也就是说父进程 ...
- 【通用邮件发送】C# QQ 网易邮箱
using BooksStore.Domain.Models; using System; using System.Collections.Generic; using System.Linq; u ...
- 杭电多校第七场-J-Sequence
题目描述 Let us define a sequence as belowYour job is simple, for each task, you should output Fn module ...
- 图论:最短路-Dijkstra
Dijkstra+堆优化具有稳定的时间复杂度,在一些数据范围要求比较严格(准确来说是图比较苛刻)的时候能够保证稳定的时间复杂度 但是Dijkstra不能够解决负边权的问题,所以在使用的时候一定要仔细读 ...
- linux shell读取配置文件
配置文件CoverageInfo FTP_URL=ftp://svn-fb.sicent.com:21/jenkins/Jifei_Repo/OL-2/IDC_Platform/bar_seats_c ...