codeforces educational round 32 F Connecting Vertices

F. Connecting Vertices
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
 
 
 

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

 用dp[i][j][1]表示连接(i, j)之间所有点且这些点再连i或j的方案数,dp[i][j][0]表示连接(i, j)之间所有点且这些点再连接j的方案数。

则先遍历(i, j)内与j相连的点y,dp[i][j][0] += dp[i][y][0] * dp[y][j][1];

再遍历(i, j)内与i相连的点x,dp[i][j][1] += dp[i][x][1] * dp[x][j][1], 结束后dp[i][j][1] += dp[i][j][0]。

引入一个与1相同的点n + 1,则dp[1][n + 1][0]为答案。时间复杂度大约O(1 / 6 * n^3 + n ^ 2 * log(n) )。

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <iomanip>
#include <cctype>
#include <cassert>
#include <bitset>
#include <ctime>

using namespace std;

#define pau system("pause")
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define clr(a, x) memset(a, x, sizeof(a))

const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

int n, x, mmp[515][515];
vector<int> edge[515];
ll dp[515][515][2];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            scanf("%d", &mmp[i][j]);
        }
    }
    for (int i = 1; i <= n; ++i) {
        mmp[i][n + 1] = mmp[n + 1][i] = mmp[i][1];
    }
    mmp[1][n + 1] = 1;
    for (int i = 1; i <= n + 1; ++i) {
        for (int j = i + 1; j <= n + 1; ++j) {
            if (mmp[i][j]) {
                edge[i].pb(j);
                edge[j].pb(i);
            }
        }
    }
    /*for (int i = 1; i <= n + 1; ++i) {
        for (int j = 0; j < edge[i].size(); ++j) {
            cout << edge[i][j] << ' ';
        }
        cout << endl;
    }*/
    for (int i = 1; i <= n; ++i) {
        dp[i][i + 1][0] = dp[i][i + 1][1] = 1;
    }
    for (int l = 2; l <= n; ++l) {
        for (int i = 1; i <= n + 1 - l; ++i) {
            int j = i + l;
            int p = lower_bound(edge[j].begin(), edge[j].end(), i + 1) - edge[j].begin();
            while (p < edge[j].size()) {
                int x = edge[j][p];
                if (x >= j) break;
                dp[i][j][0] += dp[i][x][0] * dp[x][j][1] % MOD;
                ++p;
            }
            dp[i][j][1] = (dp[i][j][0] %= MOD);
            p = lower_bound(edge[i].begin(), edge[i].end(), i) - edge[i].begin();
            while (p < edge[i].size()) {
                int x = edge[i][p];
                if (x >= j) break;
                dp[i][j][1] += dp[i][x][1] * dp[x][j][1] % MOD;
                ++p;
            }
            dp[i][j][1] %= MOD;
        }
    }
    /*if (20 == n) {
        for (int i = 11; i <= 20; ++i) {
            for (int j = 1; j <= 20; ++j) {
                printf("%d ", mmp[i][j]);
            }
            puts("");
        }
    }*/
    /*for (int l = 1; l <= n; ++l) {
        for (int i = 1; i <= n + 1 - l; ++i) {
            int j = i + l;
            for (int k = 0; k < 2; ++k) {
                printf("dp[%d][%d][%d] = %lld ", i, j, k, dp[i][j][k]);
            }
            puts("");
        }
        puts("");
    }*/
    printf("%I64d", dp[1][n + 1][0]);
    return 0;
}

  

原文地址:https://www.cnblogs.com/BIGTOM/p/7820437.html