[USACO 2017DEC] Barn Painting
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=5141
[算法]
树形DP
时间复杂度 : O(N)
[代码]
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + ;
const int MAXC = ;
const int P = 1e9 + ; struct edge
{
int to , nxt;
} e[MAXN << ]; int n , k , tot;
int color[MAXN] , head[MAXN];
LL f[MAXN][MAXC]; template <typename T> inline void chkmax(T &x,T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x , y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void update(T &x,T y)
{
x += y;
x %= P;
}
template <typename T> inline void mul(T &x,T y)
{
x = x * y;
x %= P;
} inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v , head[u]};
head[u] = tot;
}
inline LL dp(int u , int k , int fa)
{
if (f[u][k] != -) return f[u][k];
f[u][k] = ;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
if (color[v])
{
if (color[v] == k)
return f[u][k] = ;
else mul(f[u][k] , dp(v , color[v] , u));
} else
{
LL value = ;
for (int j = ; j <= ; j++)
if (j != k)
update(value , dp(v , j , u));
mul(f[u][k] , value);
}
}
return f[u][k];
} int main()
{ read(n); read(k);
for (int i = ; i < n; i++)
{
int x , y;
read(x); read(y);
addedge(x , y);
addedge(y , x);
}
for (int i = ; i <= k; i++)
{
int b , c;
read(b); read(c);
color[b] = c;
}
for (int i = ; i <= n; i++) f[i][] = f[i][] = f[i][] = -;
if (color[])
{
printf("%lld\n",dp( , color[] , -));
} else
{
LL ans = ;
for (int i = ; i <= ; i++) update(ans , dp( , i , -));
printf("%lld\n",ans);
} return ;
}
[USACO 2017DEC] Barn Painting的更多相关文章
- [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 ...
- 「日常训练」「小专题·USACO」 Barn Repair(1-4)
题意 之后补. 分析 这题同样也很精巧.我们不妨思考一下,如果只允许用一块木板,那么要购买多少距离?是整个的距离吗?不是,是从第一个到最后一个(哈哈哈哈哈哈哈).但是,不包括第一个的"左边& ...
- [USACO 2017DEC] Greedy Gift Takers
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5139 [算法] 二分答案 时间复杂度 : O(NlogN^2) [代码] #incl ...
- [USACO 2017DEC] Haybale Feast
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5142 [算法] 首先用RMQ预处理S数组的最大值 然后我们枚举右端点 , 通过二分求 ...
- [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 ...
- Luogu4084 [USACO17DEC]Barn Painting (树形DP)
数组越界那个RE+WA的姹紫嫣红的... 乘法原理求种类数,类似于没有上司的舞会. #include <iostream> #include <cstdio> #include ...
- [学习笔记]树形dp
最近几天学了一下树形\(dp\) 其实早就学过了 来提高一下打开树形\(dp\)的姿势. 1.没有上司的晚会 我的人生第一道树形\(dp\),其实就是两种情况: \(dp[i][1]\)表示第i个人来 ...
- 我的刷题单(8/37)(dalao珂来享受切题的快感
P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and S ...
随机推荐
- css中background-image背景图片路径设置
web项目中经常会用到background-image:url(),很多小伙伴不知道该怎么写需要的图片路径. 在此之前先要知道几个重要的东东: / 项目根目录 这个不用多说,就是程序 ...
- HDU2069-Coin Change
Coin Change 这题题意和UVA674很像,但加了一个限制条件不能超过100个硬币.于是我们可以用d[i][j]来表示硬币数量为i时金钱总数为j时的方法总数,总钱不能超过250. const ...
- mybatis使用步骤
1.创建config.xml文件.设置环境.数据源等: 2.设置mapper.xml文件.写sql:下面图中的resultType属性经常会替换为resultMap,不过需要加入<resultM ...
- 用svn下载github中指定目录的文件
1.先用命令看看github的分支 svn ls https://github.com/BlueRiverInteractive/robovm-ios-bindings 输出: branches/ t ...
- 贪婪大陆(cogs 1008)
[题目描述] 面对蚂蚁们的疯狂进攻,小FF的Tower defense宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海,前方是变异了的超级蚂蚁. ...
- linux命令1——基础
Rm 删除命令 Rm [选项][文件] 删除一个文件或者目录 选项:r 递归的删除文件夹及其子文件,f 忽略不存在的文件(不提示) (2)rm删除目录下所有文件,但不删除目录 >>rm - ...
- 通过分析exevc系统调用处理过程来理解Linux内核如何装载和启动一个可执行程序
前言说明 本篇为网易云课堂Linux内核分析课程的第七周作业,本次作业我们将具体来分析exec*函数对应的系统调用处理过程,来分析Linux内核如何来执行一个可执行程序,由于有一个在网易云课堂共同学习 ...
- 编译.net .net Core程序 代码,仅做备份
//创建一个ProcessStartInfo对象 使用系统shell 指定命令和参数 设置标准输出 //编译.net core项目 var psi = new ProcessStartInfo(&qu ...
- JavaScript高级程序设计重点(一)
1.一个完整的 JavaScript 实现应该由下列三 个不同的部分组成 核心(ECMAScript) 文档对象模型(DOM) 浏览器对象模型(BOM) 2.Undefined 类型只有一 ...
- Spring Boot项目错误:Error parsing lifecycle processing instructions
pom.xml文件错误:Error parsing lifecycle processing instructions 解决方法:清空.m2/repository下的所有依赖文件,重新下载即可解决该问 ...