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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 基于MATLAB实现的有限差分法实验报告用MATLAB中的有限差分法计算槽内电位;对比解析法和数值法的异同点;选取一点,绘制收敛曲线;总的三维电位图+使用说明文档 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2020b;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细); 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可后台私信博主; 4.1 期刊或参考文献复现 4.2 Matlab程序定制 4.3 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 5、欢迎下载,沟通交流,互相学习,共同进步!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值