[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 ...
随机推荐
- Kubernetes网络设计原则
在配置集群网络插件或者实践K8S 应用/服务部署请时刻想到这些原则: 1.每个Pod都拥有一个独立IP地址,Pod内所有容器共享一个网络命名空间 2.集群内所有Pod都在一个直接连通的扁平网络中,可通 ...
- [luoguP2513] [HAOI2009]逆序对数列(DP)
传送门 f[i][j]表示前i个数,逆序对数为j的答案 则DP方程为: f[1][0] = 1; for(i = 2; i <= n; i++) for(j = 0; j <= m; j+ ...
- [HDU2896]病毒侵袭(AC自动机)
传送门 题目中文描述,赞! 除了val记录id以外就是模板. 注意:每次数组都要清0.0 ——代码 #include <cstdio> #include <queue> #in ...
- SQL Server 创建唯一约束sql语句
SQL Server 创建唯一约束sql语句 语句示例: 在创建表是时同时创建, 创建id,name,sex三个字段的唯一索引 create table t1( id int primary ...
- CodeVs1519 过路费
题目描述 Description 在某个遥远的国家里,有 n个城市.编号为 1,2,3,…,n.这个国家的政府修建了m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市T需要收取的过路费 ...
- 2017"百度之星"程序设计大赛 - 初赛(B)度度熊的交易计划
n个村庄m条带权路,权值为花费,村庄可以造东西卖东西,造完东西可以换地方卖,给出每个村庄造东西花费a和最多个数b.卖东西价值c和最多个数d,求最大收益. 裸的费用流.然而还WA了一发.很好. 建源向每 ...
- Solr Admin管理界面使用说明
Notice:本说明基于Solr6.4.2. 本文讨论的是如何使用Solr Admin UI. 一级菜单 图1.SolrCloud模式 图2.单机Solr模式 Logging:展示Solr的日志,不用 ...
- hdu 4923 Room and Moor [ 找规律 + 单调栈 ]
传送门 Room and Moor Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- 导师高茂源:用CODEX创新方法破解西方创新“秘密”(转)
高茂源,“CODEX创新体系”的创立者,精一学社的创业导师.“CODEX”是Copy.Optimize.Dimension.Ecosystem.Extra五个单词的缩写,该体系精炼了现在世界上流行的创 ...
- spring mvc技术
spring mvc之访问路径 1. @RequestMapping这个注解 在实际项 ...