zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    [整机笔记][转贴]硬盘无法双击打开的解决办法
    [网络随摘][转载]值得用一生回味的经典语录
    [网络随摘][转载]如果你已经过了20还不到25岁
    [网络随摘][转摘]只有十句话,我却看了十分钟
    实现百台手机异步并发定时自动GPS定位打卡,基于python全天后定时签到稳定版。
    【FireFox】在Firefox中,关于隐藏table中某一行tr,其他td的边框显示异常
    【记】屏蔽浏览器shift+鼠标滚轴事件
    【记】Javascript的函数直接量定义
    【Javascript】Javascript中的函数调用模式
    【记】three.js的一个简单的代码记录
  • 原文地址:https://www.cnblogs.com/shaokele/p/9360871.html
Copyright © 2011-2022 走看看