Codeforces 888F. Connecting Vertices (Educational Codeforces Round 32 F. Connecting Vertices)

11 篇文章 0 订阅

F. Connecting Vertices

There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw n - 1 segments, each connecting any two marked points, in such a way that all points have to be connected with each other (directly or indirectly).

But there are some restrictions. Firstly, some pairs of points cannot be connected directly and have to be connected undirectly. Secondly, the segments you draw must not intersect in any point apart from the marked points (that is, if any two segments intersect and their intersection is not a marked point, then the picture you have drawn is invalid).

How many ways are there to connect all vertices with n - 1 segments? Two ways are considered different iff there exist some pair of points such that a segment is drawn between them in the first way of connection, but it is not drawn between these points in the second one. Since the answer might be large, output it modulo 109 + 7.

Input

The first line contains one number n (3 ≤ n ≤ 500) — the number of marked points.

Then n lines follow, each containing n elements. ai, j (j-th element of line i) is equal to 1 iff you can connect points i and j directly (otherwise ai, j = 0). It is guaranteed that for any pair of points ai, j = aj, i, and for any point ai, i = 0.

Output

Print the number of ways to connect points modulo 109 + 7.

Examples
input
3
0 0 1
0 0 1
1 1 0
output
1
input
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
output
12
input
3
0 0 0
0 0 1
0 1 0
output
0


题意:

有n个点,给你邻接矩阵表示第i中第j列这两个点i与j是否可以直接相连,a[i][j]=1表示可以直接相连。前提是你必须使用了n-1条边,如果用不完或者不够都是不能满足题意的,

所以输出0。现在问你对于给定的一组关系,满足题意的连接方案数


AC code

#include<bits/stdc++.h>
#define mod 1000000007
#define Maxn 507
using namespace std;

int n,a[Maxn][Maxn],d[Maxn][Maxn],f[Maxn][Maxn];

int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		for (int j=0;j<n;j++)
			scanf("%d",&a[i][j]);

	for(int i=0;i<n;i++)
		d[i][i]=1,f[i][i]=1;

	for(int len=2;len<=n;len++)
	{
        for(int i=0;i<n;i++)
		{
			int j=(i+len-1)%n;
			for(int k=i;k!=j;k=(k+1)%n)
			{
				if(a[i][j])
                    d[i][j]=(d[i][j]+1LL*f[i][k]*f[(k+1)%n][j])%mod;
				f[i][j]=(f[i][j]+1LL*d[i][(k+1)%n]*f[(k+1)%n][j])%mod;
			}
		}
	}
	printf("%d\n",f[0][n-1]);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值