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

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章