Educational Codeforces 888F(区间dp)

www.cnblogs.com/shaokele/


F. Connecting Vertices##

  Time Limit: 4 Sec
  Memory Limit: 256 MB

Description###

  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;
}
posted @ 2018-07-24 16:38  skl_win  阅读(291)  评论(0编辑  收藏  举报
Live2D