[CC-BLREDSET]Black and Red vertices of Tree
[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的更多相关文章
- BNUOJ 26229 Red/Blue Spanning Tree
Red/Blue Spanning Tree Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on HDU. ...
- CF375E Red and Black Tree(线性规划)
CF375E Red and Black Tree(线性规划) Luogu 题解时间 很明显有一个略显复杂的 $ n^3 $ dp,但不在今天讨论范围内. 考虑一些更简单的方法. 设有 $ m $ 个 ...
- [Codeforces375E]Red and Black Tree
Problem 给定一棵有边权的树.树上每个点是黑或白的.黑白点能两两交换. 求符合任意一个白点到最近黑点的距离小于等于x时,黑白点交换次数最少为多少. Solution 明显是一题树形DP.我们先跑 ...
- [CodeForces-375E]Red and Black Tree
题目大意: 给你一棵带边权的树,每个结点可能是红色或者黑色,你可以交换若干个点对使得任意一个红点到达与其最近的黑点的距离小于等于m. 思路: 动态规划. f[i][j][k]表示以i为根的子树中,连向 ...
- 「CF375E」Red and Black Tree「树形DP」
题意 给定一个结点颜色红或黑的树,问最少进行多少次交换黑.红结点使得每个红结点离最近的黑结点距离\(\leq x\). \(1\leq n \leq 500, 1 \leq x \leq 10^9\) ...
- 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 ...
- 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 ...
- 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 ...
- easyui 键盘控制tree 上下
$.extend($.fn.tree.methods, { highlight: function(jq, target){ return jq.each(function(){ $(this).fi ...
随机推荐
- Datagrip导入导出为一个sql文件详细说明 (mysql)
Datagrip导入导出为一个sql文件详细说明 导出: (我的无法截图, 借用一张) 导入: (我的无法截图, 再借用一张) 完
- 将眼底图片生成的txt文件进行格式化处理
# -*- coding: utf-8 -*- """ 将图片转换生成的txt文件进行格式化处理 """ import os import ...
- 升级 Apache Tomcat的办法
1.下载最新的7系列tomcat cd /usr/local/software wget https://www-us.apache.org/dist/tomcat/tomcat-7/v7.0.92/ ...
- https://www.cnblogs.com/zoro-robin/p/6110188.html
https://www.cnblogs.com/zoro-robin/p/6110188.html https://blog.csdn.net/kongxx/article/details/65435 ...
- PHP中self和this的用法区别
PHP支持类和面向对象结构,PHP的类的静态函数和变量不与任何特定类的实例相关联(换句话说,一个对象).请看:类与对象的区别. 相反,静态函数和变量与类定义本身相关联.换言之,一个类的所有实例都共享相 ...
- Java连接MySQL数据库三种方法
好久没有更新博客了!今天利用周目时学习了一下数据库mysql.介绍一下数据库的三种连接方式! 开发工具:Myeclipse MySQL5.6 MySQL连接驱动:mysql-connector-jav ...
- noip2017逛公园
题解: 之前知道正解并没有写过.. #include <bits/stdc++.h> using namespace std; #define rint register int #def ...
- python导入import
1.参考 Python 相对导入与绝对导入 2.Python import 的搜索路径 在当前目录下搜索该模块 在环境变量 PYTHONPATH 中指定的路径列表中依次搜索 在 Python 安装路径 ...
- Codeforces 316E3 线段树 + 斐波那切数列 (看题解)
最关键的一点就是 f[ 0 ] * a[ 0 ] + f[ 1 ] * a[ 1 ] + ... + f[ n - 1] * a[ n - 1] f[ 1 ] * a[ 0 ] + f[ 2 ] * ...
- unicode utf-8 ascll编码比较
1.字符编码 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte), 所以,一个字节能表示的最大的整数就 ...