[CC-BLREDSET]Black and Red vertices of Tree

题目大意:

有一棵\(n(\sum n\le10^6)\)个结点的树,每个结点有一种颜色(红色、黑色、白色)。删去一个由红色点构成的连通块,使得存在一个黑点和一个白点,满足这两个点不连通。问有多少种删法。

思路:

设满足删掉这个点后,使得存在一个黑点和一个白点,满足这两个点不连通的红点为关键点。那么我们可以用两个\(\mathcal O(n)\)的树形DP求出所有的关键点。剩下的问题就变成了求有多少种全红连通块使得该连通块中至少有一个关键点,这显然又可以用一个\(\mathcal O(n)\)树形DP求出。

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,mod=1e9+7;
bool mark[N];
int col[N],cnt1[N],cnt2[N],f[N][2];
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
void dfs(const int &x,const int &par) {
cnt1[x]=cnt2[x]=0;
if(col[x]==1) cnt1[x]=1;
if(col[x]==2) cnt2[x]=1;
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs(y,x);
cnt1[x]+=cnt1[y];
cnt2[x]+=cnt2[y];
}
}
void move(const int &x,const int &par) {
bool g1=false,g2=false;
if(x!=1) {
g1=cnt1[par]-cnt1[x];
g2=cnt2[par]-cnt2[x];
cnt1[x]+=cnt1[par]-cnt1[x];
cnt2[x]+=cnt2[par]-cnt2[x];
}
mark[x]=false;
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
mark[x]|=cnt1[y]&&g2;
mark[x]|=cnt2[y]&&g1;
g1|=cnt1[y];
g2|=cnt2[y];
move(y,x);
}
}
void dp(const int &x) {
col[x]=-1;
f[x][mark[x]]=1;
f[x][!mark[x]]=0;
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i];
if(col[y]) continue;
dp(y);
f[x][1]=(1ll*f[x][1]*(f[y][0]+f[y][1]+1)%mod+1ll*f[x][0]*f[y][1]%mod)%mod;
f[x][0]=1ll*f[x][0]*(f[y][0]+1)%mod;
}
}
int main() {
for(register int T=getint();T;T--) {
const int n=getint();
for(register int i=1;i<n;i++) {
add_edge(getint(),getint());
}
for(register int i=1;i<=n;i++) {
col[i]=getint();
}
dfs(1,0);
move(1,0);
for(register int i=1;i<=n;i++) {
if(!col[i]) dp(i);
}
for(register int i=1;i<=n;i++) {
e[i].clear();
}
int ans=0;
for(register int i=1;i<=n;i++) {
if(col[i]==-1) (ans+=f[i][1])%=mod;
}
printf("%d\n",ans);
}
return 0;
}

[CC-BLREDSET]Black and Red vertices of Tree的更多相关文章

  1. BNUOJ 26229 Red/Blue Spanning Tree

    Red/Blue Spanning Tree Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on HDU. ...

  2. CF375E Red and Black Tree(线性规划)

    CF375E Red and Black Tree(线性规划) Luogu 题解时间 很明显有一个略显复杂的 $ n^3 $ dp,但不在今天讨论范围内. 考虑一些更简单的方法. 设有 $ m $ 个 ...

  3. [Codeforces375E]Red and Black Tree

    Problem 给定一棵有边权的树.树上每个点是黑或白的.黑白点能两两交换. 求符合任意一个白点到最近黑点的距离小于等于x时,黑白点交换次数最少为多少. Solution 明显是一题树形DP.我们先跑 ...

  4. [CodeForces-375E]Red and Black Tree

    题目大意: 给你一棵带边权的树,每个结点可能是红色或者黑色,你可以交换若干个点对使得任意一个红点到达与其最近的黑点的距离小于等于m. 思路: 动态规划. f[i][j][k]表示以i为根的子树中,连向 ...

  5. 「CF375E」Red and Black Tree「树形DP」

    题意 给定一个结点颜色红或黑的树,问最少进行多少次交换黑.红结点使得每个红结点离最近的黑结点距离\(\leq x\). \(1\leq n \leq 500, 1 \leq x \leq 10^9\) ...

  6. 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)

    BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...

  7. ACM-ICPC2018 青岛赛区网络预赛-B- Red Black Tree

    题目描述 BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. A ...

  8. 1443. Minimum Time to Collect All Apples in a Tree

    Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in t ...

  9. easyui 键盘控制tree 上下

    $.extend($.fn.tree.methods, { highlight: function(jq, target){ return jq.each(function(){ $(this).fi ...

随机推荐

  1. Vue 添加外部的时间插件不触发v-model事件更改数据

    使用的jquery日期插件 最终问题是 在选择完成日期后并未激活 oninput事件,所以也没有激活v-model 去改变date 解决思路: 去插件js文件中,在赋值给dom的时候添加模拟输入事件便 ...

  2. vetur插件提示 [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives错误的解决办法

    错误提示: [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives.Renders the ...

  3. 根据id查询所有子节点/父节点,mysql 以及ssm前后台处理流程

    1.所示案例数据表结构设计如下所示: 2.案例数据如下所示: 3.mysql查询语句可以查询出父级目录信息: 注意:自己的数据表表名称,切记手动修改,字段名称(特别注意id,parent_id字段名称 ...

  4. OpenGL的gl.h出现一堆错误,如重定义什么的

    问题:生成时提示 gl.h中出现一堆错误,如 error C2144: 语法错误 : "void"的前面应有";" error C2182: "API ...

  5. ajax后台输出有红点

    转自 百度了很多,说有utf8的bom头,通过dw,sublime软件,各种清除格式无果. 后来直接在返回结果之前,执行一下ob_clean(); 完美解决问题

  6. python从零安装

    一 python 1.安装python https://www.python.org/ 环境变量path添加 ;C:\Python27;C:\Python27\Lib\site-packages;C: ...

  7. WINDOWS 2008Server 配置nginx 反向代理服务器 安装成服务

    本案例有用过可行 反向代理就是是网站通过一台机器发布到公网,客户访问的时候是直接访问那台代理机器的,然后通过那台机器才访问到内网网站.   0.先要在域名官网上面配置域名对应的IP地址,然后要在自己路 ...

  8. 基于springboot通过自定义注解和AOP实现权限验证

    一.移入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  9. gitlab之六: gitlab 备份恢复

    参考:   https://blog.csdn.net/ouyang_peng/article/details/77070977 备份: 所有的权限,库文件等信息全部备份到的 不更改备份目录的话: v ...

  10. 函数对象的call()、apply() 方法区别

    函数对象的call().apply() 方法 函数作为对象提供了call(),apply() 方法,他们也可以用来调用函数,这两个方法都接受一个对象作为参数,用来指定本次调用时函数中this的指向: ...