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;
    }
    
  • 相关阅读:
    kafka常见问题汇总
    kafka可视化工具kafkatool
    VB.NET DevExpress GirdView 搜素框界面Find Clear按钮转换为自定义中文
    winform DevExpress GridView复制单元格方法
    DevExPress GridView获取单元格坐标和内容
    Winform Log4Net使用(一)(产生yyyyMMdd'.log)便于每天使用记录一眼能看出哪天使用时出错
    winform 判断重复检测,是否开启相同应用程序 和 线程异常捕获
    winfrom Run状态控件刷新办法
    C# winform Panel自定义移动窗口
    C# 控制台CMD辅助类
  • 原文地址:https://www.cnblogs.com/shaokele/p/9360871.html
Copyright © 2011-2022 走看看