[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 ...
随机推荐
- 检查URL的可用性脚本
#!/bin/bash check_url() { HTTP_CODE=$(curl -o /dev/ -s -) ];then echo "Warning: $1 Access failu ...
- python压缩文件
#coding=utf-8 #压缩文件 import os,os.path import zipfile #压缩:传路径,文件名 def zip_compression(dirname,zipfile ...
- spring cloud 声明式rest客户端feign调用远程http服务
在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.Feign就是Spring Cloud提供的一种声明式R ...
- VOC数据集生成代码使用说明
#split.py 文件 输入格式为images ,和标签txt文件,txt中的数据为坐标值共8个. import os import numpy as np import math import c ...
- 步步為營-98-MyAPI
1 通过NuGet程序管理包添加 Microsoft Asp.Net webAPI 2.2 的引用 2 添加两个文件夹Controllers和Models 2.1 在本地模拟数据库,所以在Model ...
- Python 9*9口诀
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:Liuyoushui # Time = 2017/7/18 10:33 print ('\n ...
- SQL Server等待
等待大概分为3类:资源等待.队列等待.外部等待 过滤掉系统相关的等待类型的语句.(查看常用的等待信息) SELECT wait_type , signal_wait_time_ms , wait_ti ...
- 基于Redis的分布式锁到底安全吗
http://zhangtielei.com/posts/blog-redlock-reasoning.html
- Redis-Sentinel 哨兵
为什么需要哨兵? 一旦主节点宕机,那么需要人为修改所有应用方的主节点地址(改为新的master地址),还需要命令所有从节点复制新的主节点 那么这个问题,redis-sentinel就可以解决了 什么是 ...
- URL地址编码和解码
0. 参考 [整理]关于http(GET或POST)请求中的url地址的编码(encode)和解码(decode) python3中的urlopen对于中文url是如何处理的? 中文URL的编码问题 ...