• 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
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)
    【读书笔记《Android游戏编程之从零开始》】5.Android 游戏开发常用的系统控件(ProgressBar、Seekbar)
    【读书笔记《Android游戏编程之从零开始》】4.Android 游戏开发常用的系统控件(EditText、CheckBox、Radiobutton)
    【读书笔记《Android游戏编程之从零开始》】3.Android 游戏开发常用的系统控件(Button、Layout、ImageButton)
    【读书笔记《Android游戏编程之从零开始》】2.Hello,World!
    【读书笔记《Android游戏编程之从零开始》】1.Android 平台简介与环境搭建
    【Android Demo】通过WebService获取今日天气情况
    布局优化之ViewStub源码分析
    http2.0与http1.X的区别
    OkHttpHelper使用
  • 原文地址:https://www.cnblogs.com/shaokele/p/9360871.html
Copyright © 2020-2023  润新知