Educational Codeforces 888F(区间dp)
www.cnblogs.com/shaokele/
F. Connecting Vertices##
Time Limit: 4 Sec
Memory Limit: 256 MBDescription###
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 \(10^9 + 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. \(a_{i, j}\) (\(j\)-th element of line \(i\)) is equal to \(1\) iff you can connect points \(i\) and \(j\) directly (otherwise \(a_{i, j} = 0\)). It is guaranteed that for any pair of points \(a_{i, j} = a_{j, i}\), and for any point \(a_{i, i} = 0\).
Output###
Print the number of ways to connect points modulo \(10^9 + 7\).
Sample Input 1
3
0 0 1
0 0 1
1 1 0
Sample Output 1
1
Sample Input 2
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
Sample Output 2
12
Sample Input 3
3
0 0 0
0 0 1
0 1 0
Sample Output 3
0
题目地址: Educational Codeforces 888F
题目大意:
一共n个点
把它们连成一棵树
对于第 \(i\) 个点连第 \(j\) 个点,第 \(k\) 个点连第 \(l\) 个点
如果有 \(i<k<j<l\) 是不允许的
问允许的方案数
题解:
有点难的区间dp
官方题解
我的做法和官方题解有些许不同
\(f[i][j]\)表示 \(i\) 到 \(j\) 有边且构成树的方案数
\(g[i][j]\) 表示 \(i\) 到 \(j\) 无边且构成树的方案数
转移:枚举 \(i,j\) 左右端点, \(k\) 是断点
$$ f[i][j]+=\sum_i^{j-1}(f[i][k]+g[i][k])(f[k+1][j]+g[k+1][j])$$
$$ g[i][j]+=\sum_{i+1}^{j-1}f[k][j](f[i][k]+g[i][k])$$
初始化只要 \(f[i][j]=1\) 就可以了
AC代码
#include <cstdio>
#define ll long long
using namespace std;
const int N=305,Mod=998244353;
int n;
int a[N][N],f[N][N],g[N][N];
void add(int &a,int b){
a+=b;if(a>=Mod)a-=Mod;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)f[i][i]=1;
//f[i][j]表示i到j有边且构成树的方案数
//g[i][j]表示i到j无边且构成树的方案数
for(int len=2;len<=n;len++)
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
for(int k=i;k<j;k++)
if(a[i][j])
add(f[i][j],1ll*(f[i][k]+g[i][k])*(f[k+1][j]+g[k+1][j])%Mod);
for(int k=i+1;k<j;k++)
if(a[k][j])
add(g[i][j],1ll*f[k][j]*(f[i][k]+g[i][k])%Mod);
}
int ans=0;
add(ans,f[1][n]);
add(ans,g[1][n]);
printf("%d\n",ans);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步