[USACO17DEC]Barn Painting (树形$dp$)
题目链接
Solution
比较简单的树形 \(dp\) 。
\(f[i][j]\) 代表 \(i\) 为根的子树 ,\(i\) 涂 \(j\) 号颜色的方案数。
转移很显然 :
\]
其中 \(k\) 代表它的子节点。 其他两种颜色以此类推。
但需要注意的是对于颜色固定的点,除固定的颜色外,其他两种颜色的方案要赋为 \(0\) 。
PS : 要开 longlong ,以及还要 mod 1000000007 。
Code
#include<bits/stdc++.h>
#define N 100010
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
using namespace std;
void in(ll &x)
{
char ch=getchar();ll f=1,w=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
x=f*w; return;
}
struct sj{
ll to,next;
}a[N*2];
ll head[N],size;
ll n,col[N],k;
void add(ll x,ll y)
{
a[++size].to=y;
a[size].next=head[x];
head[x]=size;
}
ll f[N][4];
void dfs(ll x,ll fr)
{
f[x][1]=f[x][2]=f[x][3]=1;
for(ll i=head[x];i;i=a[i].next)
{
ll tt=a[i].to;
if(tt==fr)continue;
dfs(tt,x);
f[x][1]*=(f[tt][2]+f[tt][3])%mod;
f[x][1]%=mod;
f[x][2]*=(f[tt][1]+f[tt][3])%mod;
f[x][2]%=mod;
f[x][3]*=(f[tt][2]+f[tt][1])%mod;
f[x][3]%=mod;
}
if(col[x])
for(int i=1;i<=3;i++)
if(col[x]!=i)f[x][i]=0;
return;
}
int main()
{
in(n); in(k);
for(ll i=1;i<n;i++)
{
ll x,y; in(x),in(y);
add(x,y),add(y,x);
}
for(ll i=1;i<=k;i++)
{
ll x,y; in(x),in(y);
col[x]=y;
}
dfs(1,0);
cout<<(f[1][1]%mod+f[1][2]%mod+f[1][3]%mod)%mod<<endl;
return 0;
}
[USACO17DEC]Barn Painting (树形$dp$)的更多相关文章
- [USACO17DEC] Barn Painting - 树形dp
设\(f[i][j]\)为\(i\)子树,当\(i\)为\(j\)时的方案数 #include <bits/stdc++.h> using namespace std; #define i ...
- [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 ...
- Luogu4084 [USACO17DEC]Barn Painting (树形DP)
数组越界那个RE+WA的姹紫嫣红的... 乘法原理求种类数,类似于没有上司的舞会. #include <iostream> #include <cstdio> #include ...
- Codeforces - 1198D - Rectangle Painting 1 - dp
https://codeforces.com/contest/1198/problem/D 原来是dp的思路,而且是每次切成两半向下递归.好像在哪里见过类似的,貌似是紫书的样子. 再想想好像就很显然的 ...
- ZOJ-3725 Painting Storages DP
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3725 n个点排列,给每个点着色,求其中至少有m个红色的点连续的数 ...
- [USACO 2017DEC] Barn Painting
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5141 [算法] 树形DP 时间复杂度 : O(N) [代码] #include< ...
- [CodeForces 300D Painting Square]DP
http://codeforces.com/problemset/problem/300/D 题意:每一次操作可以选一个正方形,令边长为n,如果n为奇数那么可以从中间画一个十字,分成4个大小相等的边长 ...
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
- 我的刷题单(8/37)(dalao珂来享受切题的快感
P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and S ...
随机推荐
- HttpModule 介绍
引言 Http 请求处理流程 和 Http Handler 介绍 这两篇文章里,我们首先了解了Http请求在服务器端的处理流程,随后我们知道Http请求最终会由实现了IHttpHandler接口的类进 ...
- Java ——String 类 StringBuffer 和 StringBuilder类 随机字符
本节重点思维导图 String 类 创建字符串 String str = "I love ai"; 在代码中遇到字符串常量时,"I love ai",编译器会使 ...
- python接口自动化:调试接口的代码(无token情况下)
实现代码如下: #接口调试 import requests,time class api_demo1: def __init__(self,RequestWay,url,data): self.s=r ...
- docker搭建一个渗透测试环境 bwapp为例
bwapp是一个渗透测试靶场,他其中中含有100多个Web漏洞 基本涵盖了所有主要的已知Web漏洞,包括OWASP Top 10的各种 首先要去搜索一下 看一下有哪些镜像可以下载 docke ...
- 在使用spring中的ContextConfiguration、test注解时出现的错误
错误: 在使用测试注解时出现ContextConfiguration注解和test注解无法正常导包使用的编译异常,如图: 解决办法: 将pom.xml文件中以下依赖管理 中的<scope> ...
- Maven 修改jdk版本
Maven 修改jdk版本方法1: <build> <plugins> <plugin> <groupId>org.apache.maven.plugi ...
- [Python3] 011 字符串:给你们看看我的内置方法 第三弹
目录 少废话,上例子 1. encode(encoding='utf-8', errors='strict') 2. expandtabs([tabsize=8]) 借此机会简单地说一说 print( ...
- Centos 7下Hadoop分布式集群搭建
一.关闭防火墙(直接用root用户) #关闭防火墙 sudo systemctl stop firewalld.service #关闭开机启动 sudo systemctl disable firew ...
- solr 安装与配置
1. Solr安装与配置 1.1什么是Solr 大多数搜索引擎应用都必须具有某种搜索功能,问题是搜索功能往往是巨大的资源消耗并且它们由于沉重的数据库加载而拖垮你的应用的性能. 这就是为什么转移负载到一 ...
- ARM汇编2
一. 数据处理汇编指令 1.1. 数据传输指令 1.1.1. mov(Move) 指令 示例:mov r0, r0, LSL#3 ; r0 = r0* 8 mov pc, r ...