In Chinese Restaurant

题目连接:

http://acm.hust.edu.cn/vjudge/contest/123332#problem/B

Description

When Vova arrived in Guangzhou, his Chinese friends immediately invited him to a restaurant. Overall n people came to the restaurant, including Vova. The waiter offered to seat the whole company at a traditional large round table with a rotating dish plate in the center.

As Vova was a guest, he got the honorable place by the door. Then m people in the company stated that they wanted to sit near a certain person. Your task is to determine the number of available arrangements to seat Vova's friends at the table.

Input

The first line contains integers n and m (2 ≤ n ≤ 100; 0 ≤ m ≤ n). The next m lines contain integers k1, …, km, where ki is the number of the person who the person number i wants to sit with (1 ≤ ki ≤ n; ki ≠ i). Being an honorable guest, Vova got number 1. His friends are numbered with integers from 2 to n.

Output

Print the number of available arrangements to seat Vova's friends modulo 10 9 + 7.

Sample Input

6 6

2

1

1

5

6

5

Sample Output

4

Hint

题意

有n个人在坐在一个圆桌上,给你m个关系a,b,表示a要挨着b坐

问你有多少个方案数

题解:

如果出现环,答案为2,否则就是全排列乘以2

特判掉 只有两个人的情况,因为这样正着坐和反着坐答案是一样的。

代码

#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std; const int maxn = 1e3 + 15;
vector<int>G[maxn],block[maxn];
int n , m ,vis[maxn] , c[maxn],tot,sz[maxn],deg[maxn],fac[maxn],C[maxn][maxn],moo[maxn][maxn];
set < int > Edge[maxn]; void maintain(int & x){
if( x >= mod ) x-= mod;
} bool dfs1( int x , int pre ){
c[x] = 1;
for(auto v:G[x]){
if( v == pre ) continue;
if(c[v] == 1) return true;
else if(c[v] == 0){
if(dfs1(v,x)) return true;
}
}
c[x] = 2;
return false;
} void predfs( int x ){
block[tot].pb( x );
for(auto v : G[x])
if( vis[v] == 0 ){
vis[v] = 1;
predfs( v );
}
} void init(){
fac[0] = 1;
C[0][0] = 1;
rep(i,1,maxn-1){
fac[i] = mul( fac[i - 1] , i );
C[i][0] = 1;
rep(j,1,i){
C[i][j] = C[i][j - 1] + C[i - 1][j - 1] ;
maintain( C[i][j] );
}
}
} int main(int argc,char *argv[]){
init();
n=read(),m=read();
if(n == 2 && m >= 1 ){
pf("1\n");
return 0;
}
rep(i,1,n) G[i].clear();
rep(i,1,m){
int v=read();
Edge[i].insert( v );
moo[i][v]=moo[v][i]=1;
}
rep(i,1,n){
rep(j,1,n) if( moo[i][j] && i != j ) G[i].pb( j );
}
int ok = 0;
rep(i,1,n) if(c[i] == 0){
if( dfs1( i , 0 ) ){
ok = 1;
break;
}
}
rep(i,1,n) if(!vis[i]){
vis[i]=1;
++ tot;
predfs( i );
}
// 有三条边或以上
rep(i,1,n){
int kk = 0;
rep(j,1,n) if( moo[i][j]) ++ kk;
if( kk > 2 ){
pf("0\n");
return 0;
}
}
if( ok ){
if( tot == 1 ){
int judge = 1;
rep(i,1,n) if(c[i] != 1) judge = 0;
if( judge == 1 ) pf("%d\n" , 2);
else pf("0\n");
}else pf("0\n");
return 0;
}else{
int ptr = 0 ;
rep(i,1,tot){
for(auto it : block[i]){
if( it == 1 ){
ptr = i;
}
}
}
int ans = 1 ;
if( block[ptr].size() > 1 ) ans = 2;
rep(i,1,tot){
if( i == ptr ) continue;
int cap = block[i].size();
if( cap > 1 ) ans = mul( ans , 2 );
}
ans = mul( ans , fac[ tot - 1 ] );
pf("%d\n" , ans );
}
return 0;
}

URAL 1962 In Chinese Restaurant 数学的更多相关文章

  1. Distance dependent Chinese Restaurant Processes

    Here is a note of Distance dependent Chinese Restaurant Processes 文章链接http://pan.baidu.com/s/1dEk7ZA ...

  2. URAL 1725. Sold Out!(数学啊 )

    题目链接:space=1&num=1725" target="_blank">http://acm.timus.ru/problem.aspx?space= ...

  3. 中国餐馆过程(Chinese restaurant process)

    也就是说假设空桌子有a0个人,然后顾客选择桌子的概率和桌子上人数成正比. 性质: 改变用户的排列方式,桌子的排列方式,概率不变换.

  4. URAL 1984. Dummy Guy(数学啊)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1984 1984. Dummy Guy Time limit: 0.5 second Me ...

  5. URAL 1823. Ideal Gas(数学啊 )

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1823 1823. Ideal Gas Time limit: 0.5 second Me ...

  6. [CSP-S模拟测试]:chinese(数学)

    题目传送门(内部题25) 输入格式 一行三个整数$n,m,k$. 输出格式 一行一个整数表示答案. 样例 样例输入: 2 2 2 样例输出: 数据范围与提示 样例解释: $f_0=10,f_1=4,f ...

  7. R Language

    向量定义:x1 = c(1,2,3); x2 = c(1:100) 类型显示:mode(x1) 向量长度:length(x2) 向量元素显示:x1[c(1,2,3)] 多维向量:multi-dimen ...

  8. [IR] Concept Search and LDA

    重要的是通过实践更深入地了解贝叶斯思想,先浅浅地了解下LDA. From: http://blog.csdn.net/huagong_adu/article/details/7937616/ 传统方法 ...

  9. [Bayes ML] This is Bayesian Machine Learning

    From: http://www.cnblogs.com/bayesianML/p/6377588.html#central_problem You can do it: Dirichlet Proc ...

随机推荐

  1. CSS那些事!这个篇幅是我特意开的,不是因为帮助小菜之类的,而是在多人的团队配合中各种命名冲突的规范让人蛋疼

    CSS那些事!这个篇幅是我特意开的,不是因为帮助小菜之类的,而是在多人的团队配合中各种命名冲突的规范让人蛋疼. css这个东西只要不是新的离谱都会写,但是每个人的命名风格,方法,都不同 有人喜欢驼峰, ...

  2. AngularJs -- 指令中使用子作用域

    下面将要介绍的指令会以父级作用域为原型生成子作用域.这种继承的机制可以创建一个隔离层,用来将需要协同工作的方法和数据模型对象放置在一起. ng-app和ng-controller是特殊的指令,因为它们 ...

  3. linux笔记_day12_shell编程

    1.shell中如何进行算术运算 A=1 B=2 1)let 算术运算表达式 let C=$A+$B 2)$[算术运算表达式] C=$[$A+$B] 3)$(($A+$B)) 4) expr 算术表达 ...

  4. [Android]使用 Eclipse 给 APK 签名时遇到的两个问题及解决办法

    问题 今天用 APK 反编译工具看了一下自己项目生成的 APK 文件,发现代码并没有混淆,于是设置了用 ProGuard 混淆代码,可是混淆是必须在非 Debug 模式才会生效的,即使你是以 Rele ...

  5. 用原生js实现ajax

    // 通过createXHR()函数创建一个XHR对象 function createXHR() { if(window.XMLHttpRequest) { // IE7.Firefox.Opera. ...

  6. [转载]angular通过$http与服务器通信

    转载自:http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View angular是一个前端框架,实现了可交互式的页面,但是对于一个web应 ...

  7. python日常

    1.远程访问远程访问Jupyter Notebook,本地浏览器不能打开,先查了防火墙的状态,然后将设置的端口进行allow,网址,仍然拒绝链接,而后通过远程访问Jupyter Notebook,然后 ...

  8. IntelliJ IDEA 设置Output 窗口字体大小

    Settings——>Editor——>Colors&Fonts——>Console Font 如图: 字体调好了以后使用起来眼睛就轻松多了

  9. [转] javascript组件开发方式

    作为一名前端工程师,写组件的能力至关重要.虽然JavaScript经常被人嘲笑是个小玩具,但是在一代代大牛的前仆后继的努力下,渐渐的也摸索了一套组件的编写方式. 下面我们来谈谈,在现有的知识体系下,如 ...

  10. shell学习(三)

    libvirt用于管理KVM本身的工具 virt-install用于安装虚拟机需要的安装包,安装虚拟机 virt-mananger:管理创建删除虚拟机的工具 ---恢复内容开始--- 1    gre ...