Luogu4084 [USACO17DEC]Barn Painting (树形DP)
数组越界那个RE+WA的姹紫嫣红的。。。
乘法原理求种类数,类似于没有上司的舞会。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 100007;
const int mod = 1000000007;
struct Edge{
int nxt, pre;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v){
e[++cntEdge] = (Edge){head[u], v}, head[u] = cntEdge;
}
int col[N];
long long f[N][3];
inline void DFS(int u, int fa){
if(col[u])
f[u][col[u] - 1] = 1;
else{
R(i,0,2)
f[u][i] = 1;
}
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == fa) continue;
DFS(v, u);
f[u][0] = f[u][0] * (f[v][1] + f[v][2]) % mod;
f[u][1] = f[u][1] * (f[v][0] + f[v][2]) % mod;
f[u][2] = f[u][2] * (f[v][0] + f[v][1]) % mod;
}
}
int main(){
//FileOpen();
int n, K;
io >> n >> K;
R(i,2,n){
int u, v;
io >> u >> v;
add(u, v);
add(v, u);
}
R(i,1,K){
int x, nodeColor;
io >> x >> nodeColor;
col[x] = nodeColor;
}
DFS(1, 0);
printf("%lld", ((f[1][0] + f[1][1] + f[1][2]) % mod + mod) % mod);
return 0;
}

Luogu4084 [USACO17DEC]Barn Painting (树形DP)的更多相关文章
- [USACO17DEC]Barn Painting (树形$dp$)
题目链接 Solution 比较简单的树形 \(dp\) . \(f[i][j]\) 代表 \(i\) 为根的子树 ,\(i\) 涂 \(j\) 号颜色的方案数. 转移很显然 : \[f[i][1]= ...
- [USACO17DEC] Barn Painting - 树形dp
设\(f[i][j]\)为\(i\)子树,当\(i\)为\(j\)时的方案数 #include <bits/stdc++.h> using namespace std; #define i ...
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
- [USACO17DEC] Barn Painting
题目描述 Farmer John has a large farm with NN barns (1 \le N \le 10^51≤N≤105 ), some of which are alread ...
- [学习笔记]树形dp
最近几天学了一下树形\(dp\) 其实早就学过了 来提高一下打开树形\(dp\)的姿势. 1.没有上司的晚会 我的人生第一道树形\(dp\),其实就是两种情况: \(dp[i][1]\)表示第i个人来 ...
- [USACO2002][poj1947]Rebuilding Roads(树形dp)
Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Descrip ...
- poj2378 树形DP
C - 树形dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- POJ 1947 Rebuilding Road(树形DP)
Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...
- 树形dp(poj 1947 Rebuilding Roads )
题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...
随机推荐
- MongoDB 体系结构与数据模型
每日一句 If no one else guards the world, then I will come forward. 如果没有别人保卫这个世界,那么我将挺身而出. 概述 MongoDB主要是 ...
- Random 中的Seed
C#中使用随机数 看下例 当Random的种子是0时 生成的随机数列表是一样的 也就是说当seed 一样时 审查的随机数时一样的 Random的无参实例默认 种子 时当前时间 如果要确保生成的随机数不 ...
- 2.26NK周赛
这场打的很烂,说明我对组合数学的掌握(二项式定理,以及递推式的思考方向)都不太好.而且,我做题的思路也很有问题.就是完全凭借灵感,自己没有脑子一样思路就被题目带跑了,根本跳不出来,看到题目也不会分析, ...
- 阻碍NB-IoT技术在智能水表发展的4个原因分析
与以往的机械水表不同,根据设备所搭载的模块,智能水表分为IC卡智能表.光电直读智能表以及无线远传智能表.随着物联网技术和工业的发展,无线远程传输智能水表开始被水务公司广泛使用. 以往的机械水表.指针式 ...
- 使用FileSystemWatcher监听文件状态
更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月2日. 一.FileSystemWatcher类型介绍 在.NET中使用 FileSystemWatcher 类型可以进行监视指定 ...
- ASP.NET MVC之读取服务器文件资源的两种方式
初次认识asp.net mvc时,以为所有文件都需要走一遍路由,然后才能在客户端显示, 所以我首先介绍这一种方式 比如说:我们在服务器上有图片: ~/resource/image/5.jpg 我们就需 ...
- 我用Python做了一个咖啡馆数据分析
在做案例前,我还想回答大家一个疑问,就是excel做数据分析可以实现Python一样的效果,那用Python的意义在哪呢? 经过这段时间学习理解,我的回答是: (https://jq.qq.com/? ...
- Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-b1938128a963
报错信息:Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait ...
- [BJDCTF2020]The mystery of ip|[CISCN2019 华东南赛区]Web11|SSTI注入
记录一下BUUCTF中两个类似的SSTI注入关卡 [BJDCTF2020]The mystery of ip-1: 1.打开之后显示如下: 2.在hint.php中进行了相关提示,如下: 3.既然获取 ...
- Linux for CentOS 下的 nginx 绿色安装-超省心安装
1.我这里是nginx-1.13.0-1.x86_64 .rpm(点击下载)版本的. 2.安装nginx的相应环境.有些环境可能不必须,但是安装了,确保以防万一,多多益善 yum install gd ...