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
#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


你可能感兴趣的:(DP,思维题)