[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 ...
随机推荐
- 飞行路线(BZOJ 2763)
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...
- Codevs 2666 2666 Accept Ratio
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 钻石 Diamond 题目描述 Description 某陈痴迷于pku的ACM题库,常常彻夜奋斗刷题.他最近的目标是在NOIP0 ...
- 【HDOJ6315】Naive Operations(线段树,树状数组)
题意: 两个序列a和b,初始a[i]=0,b[i]给定且为一个1到n的排列,要求维护以下两种操作:1.区间[L,R]内a[i]加1 2.询问[L,R]内a[i]/b[i](下取整)之和 n,q< ...
- poj3532求生成树中最大权与最小权只差最小的生成树+hoj1598俩个点之间的最大权与最小权只差最小的路经。
该题是最小生成树问题变通活用,表示自己开始没有想到该算法:先将所有边按权重排序,然后枚举最小边,求最小生成树(一个简单图的最小生成树的最大权是所有生成树中最大权最小的,这个容易理解,所以每次取最小边, ...
- P2863 [USACO06JAN]牛的舞会The Cow Prom
洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's ...
- 使用Spring Data Redis操作Redis(集群版)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
- mysql查询今天,昨天,近7天,近30天,本月,上一月数据的SQL
原文:http://www.open-open.com/code/view/1423207309170 select * from ad_proTrack_t where to_days(crt_ti ...
- Codeforces554E:Love Triangles
There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves B ...
- C#:excel导入导出
资源:excelService 服务 http://download.csdn.net/detail/istend/8060501 排列问题 导出时,数字和字符的排列格式默认不一样,数字靠右,字符靠左 ...
- SonarQube---在具体项目中的使用
一.简介 SonarQube(简称Sonar)是管理代码质量的开放平台,它可以快速地对代码质量进行分析,并给出合理的解决方案,提高管理效率,保证代码质量. Sonar官网,文档 Sonar Scann ...