题目链接 Coloring Brackets

考虑树型DP。(我参考了Q巨的代码还是略不理解……)

首先在序列的最外面加一对括号。预处理出DFS树。

每个点有9中状态。假设0位不涂色,1为涂红色,2为涂蓝色。

0:0 0

1:0 1

2:0 2

3:1 0

4:1 1

5:1 2

6:2 0

7:2 1

8:2 2

其中1、2、3、6为有效的状态。

DP的时候如果当前括号下没有子括号那么这个状态方案数为1。

先处理出第一对括号。然后处理接下来的括号。

拼接的时候如果出现()() 中间两个括号同时染色并且颜色相同那么该状态不合法,跳过。

转移的时候滚动一下。

如果当前节点不是根的话那么最后处理的时候只保留合法的状态。

 #include <bits/stdc++.h>

 using namespace std;

 #define LL long long

 const int valid[] = {,,,};
const LL mod = ;
const int root = ; char s[];
vector <int> e[];
stack <int> stk;
LL f[][];
int n, ret, tot; void dfs(int u){
f[u][] = ;
LL tmp[];
for (int i = , flag = ; i < (int)e[u].size(); i++){
int v = e[u][i];
dfs(v);
if (!flag){
for (int tk = (f[u][] = ); tk < ; ++tk){
int k = valid[tk];
f[u][k] = f[v][k];
} flag = ;
} else{
for (int j = ; j < ; ++j) tmp[j] = ;
for (int j = ; j < ; ++j)
for (int tk = ; tk < ; ++tk){
int k = valid[tk];
int cl[] = {j / , j % }, cr[] = {k / , k % };
if (cl[] > && cr[] > && cl[] == cr[]) continue;
int p = cl[] * + cr[];
(tmp[p] += 1LL * f[u][j] * f[v][k]) %= mod;
} for (int j = ; j < ; ++j) f[u][j] = tmp[j];
}
} if (u != root){
for (int j = ; j < ; ++j) tmp[j] = ;
for (int j = ; j < ; ++j)
for (int tk = ; tk < ; ++tk){
int k = valid[tk];
int ci[] = {j / , j % }, co[] = {k / , k % };
if ((ci[] > && co[] > && ci[] == co[])
|| (ci[] > && co[] > && ci[] == co[])) continue;
(tmp[k] +=f[u][j]) %= mod;
}
for (int j = ; j < ; ++j) f[u][j] = tmp[j];
}
} int main(){ scanf("%s", s + );
n = strlen(s + ); tot = ;
s[] = '(', s[n + ] = ')';
for (int i = ; s[i]; ++i){
if (s[i] == '('){
if (tot) e[stk.top()].push_back(tot);
stk.push(tot++);
} else stk.pop();
} dfs(root);
ret = ;
for (int i = ; i < ; ++i) (ret += f[root][i]) %= mod;
return * printf("%d\n", ret);
}

Codeforces 149D Coloring Brackets(树型DP)的更多相关文章

  1. CodeForces 149D Coloring Brackets (区间DP)

    题意: 给一个合法的括号序列,仅含()这两种.现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红.要求不能有两个同颜色的括号相邻,问有多少种染色的方法? 思路: 这题的模拟成分比较多吧?两种颜 ...

  2. codeforces 149D Coloring Brackets (区间DP + dfs)

    题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...

  3. CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)

    1.http://codeforces.com/problemset/problem/149/D 2.题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色, ...

  4. CodeForces 149D Coloring Brackets

    Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  5. CodeForces 149D Coloring Brackets 区间DP

    http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...

  6. Codeforces 23E Tree(树型DP)

    题目链接 Tree $dp[x][i]$表示以x为根的子树中x所属的连通快大小为i的时候 答案最大值 用$dp[x][j]$ * $dp[y][k]$ 来更新$dp[x][j + k]$. (听高手说 ...

  7. Codeforces 581F Zublicanes and Mumocrates(树型DP)

    题目链接  Round 322 Problem F 题意  给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...

  8. 【题解】codeforces 219D Choosing Capital for Treeland 树型dp

    题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...

  9. POJ3659 Cell Phone Network(树上最小支配集:树型DP)

    题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...

随机推荐

  1. iPhone如何设置自定义铃声?无需连接电脑,轻松几步就搞定!

    转载自: https://baijiahao.baidu.com/s?id=1594988016778457969&wfr=spider&for=pc 受够了iPhone自带的千篇一律 ...

  2. 【windows】【md5】查看文件的md5值

    certutil -hashfile filename MD5 certutil -hashfile filename SHA1 certutil -hashfile filename SHA256 ...

  3. (转).gitignore详解

    本文转自http://sentsin.com/web/666.html 今天讲讲Git中非常重要的一个文件——.gitignore. 首先要强调一点,这个文件的完整文件名就是“.gitignore”, ...

  4. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. LeetCode(228) Summary Ranges

    题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...

  6. 搜索引擎elasticsearch常用指令演示

    目录 交互方式 常用操作示例 添加文档 删除文档 修改文档 查询 简单查询 高级多条件查询 交互方式 操作ES有3种方式: kibana控制台(Dev Tools) Http + json api接口 ...

  7. MIP启发式算法:Variable Neighborhood Decomposition Search

    *本文记录和分享学习到的知识,算不上原创. *参考文献见链接. 本文主要简述和VND VNS RINS很相关的vairable neighborhood decomposition search. 目 ...

  8. LA 7049 Galaxy 枚举

    题意: \(x\)轴上有\(n\)个质量为\(1\)的点,他们的坐标分别为\(x_i\). 质心的坐标为\(\frac{\sum{x_i}} {n}\) 转动惯量为\(\sum{d_i^2}\),其中 ...

  9. luogu3193 [HNOI2008]GT考试

    there #include <iostream> #include <cstdio> using namespace std; int n, m, mod, nxt[25], ...

  10. linux随笔4

    vim编辑器: 启动vim编辑器,只需键入vim 和希望编辑的文件:vim mongo.sh 如果文件存在,将显示整个内容显示到进行编辑的缓冲区,如果文件不存在,打开一个新的缓冲区进行编辑. 内容未占 ...