zoukankan      html  css  js  c++  java
  • CF 1025 D. Recovering BST

    D. Recovering BST

    http://codeforces.com/contest/1025/problem/D

    题意:

      给出一个连续上升的序列a,两个点之间有边满足gcd(ai ,aj) != 1。选择一些边,问是否能构成一棵有n个点的二叉搜索树。

    分析: 

      区间dp。

      每个子树都是一段连续的区间,L[i][j]表示j为根,i~j-1这个区间的点能否使j的左子树,R[i][j]:i为根,i+1~j这个区间能否为i的右子树。

      枚举一个中间点作为根,转移即可。

      为什么这样转移:直接f[i][j]表示区间i~j能否构成一个树的话,还要去枚举根,或者记录根。就变成了f[i][j][k]表示区间i~j,根为k能否成为一棵树。这样复杂度就太大了。换一种记录根的方式,只记录左边和右边,两边是互不影响的,前面的状态拆成了两个,现在就不需要枚举根了。  

    代码:

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<iostream>
     6 #include<cctype>
     7 #include<set>
     8 #include<vector>
     9 #include<queue>
    10 #include<map>
    11 #define fi(s) freopen(s,"r",stdin);
    12 #define fo(s) freopen(s,"w",stdout);
    13 using namespace std;
    14 typedef long long LL;
    15 
    16 inline int read() {
    17     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
    18     for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
    19 }
    20 
    21 const int N = 705;
    22 
    23 LL a[N];
    24 bool L[N][N], R[N][N], e[N][N];
    25 
    26 LL gcd(LL a,LL b) {
    27     return b == 0 ? a : gcd(b, a % b);
    28 }
    29 
    30 int main() {
    31 
    32     int n = read();
    33     for (int i = 1; i <= n; ++i) {
    34         a[i] = read();
    35         L[i][i] = R[i][i] = 1;
    36     }
    37     for (int i = 1; i <= n; ++i) 
    38         for (int j = i + 1; j <= n; ++j) 
    39             if (gcd(a[i], a[j]) > 1) e[i][j] = e[j][i] = 1;
    40 
    41     for (int k = 1; k <= n; ++k) {
    42         for (int i = 1, j; (j = i + k - 1) <= n; ++i) {
    43             for (int mid = i; mid <= j; ++mid) {
    44                 if (L[i][mid] && R[mid][j]) 
    45                     R[i - 1][j] |= e[i - 1][mid], L[i][j + 1] |= e[mid][j + 1];
    46             }
    47         }
    48     }
    49     
    50     
    51     for (int i = 1; i <= n; ++i) {
    52         if (L[1][i] && R[i][n]) {
    53             puts("Yes"); return 0; 
    54         }
    55     }
    56     puts("No");
    57     return 0;
    58 }
  • 相关阅读:
    Evidence on the impact of sustained exposure to air pollution on life expectancy
    International Immunopharmacology/ Perinatal and early childhood environmental factors influencing allergic asthma immunopathogenesis
    Projections of the effects of climate change on allergic asthma: the contribution of aerobiology
    Word List 2
    GRE : TC21
    Grammar Mistake: What is a faulty tense sequence?
    Biology 02: Respiration
    127 super()方法详解
    128 类的多态和多态性
    126 菱形继承问题
  • 原文地址:https://www.cnblogs.com/mjtcn/p/9858202.html
Copyright © 2011-2022 走看看