Connecting Vertices (Codeforces 888F)
Tags: DP, graphs

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.


Sample Inputs:
3
0 0 1
0 0 1
1 1 0

4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0

3
0 0 0
0 0 1
0 1 0

Sample Outputs:
1

12

0

Solution (Python):
import sys
from array import array
n = int(input())
edge = [list(map(int, input().split())) for _ in range(n)]
mod = 10 ** 9 + 7
dp_f = [array('i', [-1]) * n for _ in range(n)]
dp_g = [array('i', [-1]) * n for _ in range(n)]
for i in range(n):
    dp_f[i][i] = dp_g[i][i] = 1
for i in range(n - 1):
    dp_f[i][i + 1] = dp_g[i][i + 1] = 1 if edge[i][i + 1] else 0

def f(l, r):
    if dp_f[l][r] != -1:
        return dp_f[l][r]
    dp_f[l][r] = g(l, r) if edge[l][r] else 0
    for m in range(l + 1, r):
        if edge[l][m]:
            dp_f[l][r] = (dp_f[l][r] + g(l, m) * f(m, r)) % mod
    return dp_f[l][r]

def g(l, r):
    if dp_g[l][r] != -1:
        return dp_g[l][r]
    dp_g[l][r] = f(l + 1, r)
    for m in range(l + 1, r):
        dp_g[l][r] = (dp_g[l][r] + f(l, m) * f(m + 1, r)) % mod
    return dp_g[l][r]
print(f(0, n - 1))

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u * n + v
n = int(input())
mod = pow(10, 9) + 7
a = []
for _ in range(n):
    a0 = list(map(int, input().split()))
    for i in a0:
        a.append(i)
m = n * n
(dp1, dp2) = ([0] * m, [0] * m)
for i in range(n):
    u = f(i, i)
    (dp1[u], dp2[u]) = (1, 1)
for i in range(n - 1):
    u = f(i, i + 1)
    dp1[u] = 1
    dp2[u] = 1 if a[u] else 0
for i in range(2, n):
    for l in range(n - i):
        r = l + i
        u = f(l, r)
        (dp1u, dp2u) = (0, 0)
        for c in range(l, r):
            dp1u += dp2[f(l, c)] * dp2[f(c + 1, r)] % mod
        dp1[u] = dp1u % mod
        for c in range(l + 1, r + 1):
            dp2u += a[f(l, c)] * dp1[f(l, c)] * dp2[f(c, r)] % mod
        dp2[u] = dp2u % mod
ans = dp2[f(0, n - 1)]
print(ans)

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u * n + v
n = int(input())
mod = pow(10, 9) + 7
a = []
for _ in range(n):
    a0 = list(map(int, input().split()))
    for i in a0:
        a.append(i)
m = n * n
(dp1, dp2) = ([0] * m, [0] * m)
for i in range(n):
    u = f(i, i)
    (dp1[u], dp2[u]) = (1, 1)
for i in range(n - 1):
    u = f(i, i + 1)
    dp1[u] = 1
    dp2[u] = 1 if a[u] else 0
for i in range(2, n):
    for l in range(n - i):
        r = l + i
        u = f(l, r)
        (dp1u, dp2u) = (0, 0)
        for c in range(l, r):
            dp1u += dp2[f(l, c)] * dp2[f(c + 1, r)] % mod
            dp1u %= mod
        dp1[u] = dp1u
        for c in range(l + 1, r + 1):
            dp2u += a[f(l, c)] * dp1[f(l, c)] * dp2[f(c, r)] % mod
            dp2u %= mod
        dp2[u] = dp2u
ans = dp2[f(0, n - 1)]
print(ans)