[题目链接]

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的更多相关文章

  1. [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 ...

  2. 「日常训练」「小专题·USACO」 Barn Repair(1-4)

    题意 之后补. 分析 这题同样也很精巧.我们不妨思考一下,如果只允许用一块木板,那么要购买多少距离?是整个的距离吗?不是,是从第一个到最后一个(哈哈哈哈哈哈哈).但是,不包括第一个的"左边& ...

  3. [USACO 2017DEC] Greedy Gift Takers

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5139 [算法] 二分答案 时间复杂度 : O(NlogN^2) [代码] #incl ...

  4. [USACO 2017DEC] Haybale Feast

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5142 [算法] 首先用RMQ预处理S数组的最大值 然后我们枚举右端点 , 通过二分求 ...

  5. [USACO17DEC]Barn Painting (树形$dp$)

    题目链接 Solution 比较简单的树形 \(dp\) . \(f[i][j]\) 代表 \(i\) 为根的子树 ,\(i\) 涂 \(j\) 号颜色的方案数. 转移很显然 : \[f[i][1]= ...

  6. [USACO17DEC] Barn Painting - 树形dp

    设\(f[i][j]\)为\(i\)子树,当\(i\)为\(j\)时的方案数 #include <bits/stdc++.h> using namespace std; #define i ...

  7. Luogu4084 [USACO17DEC]Barn Painting (树形DP)

    数组越界那个RE+WA的姹紫嫣红的... 乘法原理求种类数,类似于没有上司的舞会. #include <iostream> #include <cstdio> #include ...

  8. [学习笔记]树形dp

    最近几天学了一下树形\(dp\) 其实早就学过了 来提高一下打开树形\(dp\)的姿势. 1.没有上司的晚会 我的人生第一道树形\(dp\),其实就是两种情况: \(dp[i][1]\)表示第i个人来 ...

  9. 我的刷题单(8/37)(dalao珂来享受切题的快感

    P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and S ...

随机推荐

  1. [luoguP2461] [SDOI2008]递归数列(DP + 矩阵优化)

    传送门 本题主要是构造矩阵,我们只需要把那一段式子看成两个前缀和相减, 然后就直接矩阵连乘. 直接对那个k+1阶矩阵快速幂即可,注意初始化矩阵为单位矩阵,即主对角线(左上到右下)都为1其他都为0. 另 ...

  2. 【DFS+剪枝】Square

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/J [题意] 给定n个木棍,问这些木棍能否围成一个正方形 [Accepted] # ...

  3. ORACLE备份、恢复、常用查询

    --第一,启动服务,(如果数据库处于启动状态,那么略过这一步) 打开命令行执行以下语句  net start OracleServiceORCL  net start  OracleOraDb10g_ ...

  4. sequence(bzoj 1367)

    Description Input Output 一个整数R Sample Input 794820141518 Sample Output 13 HINT 所求的Z序列为6,7,8,13,14,15 ...

  5. 【Educational Codeforces Round 48】

    A:https://www.cnblogs.com/myx12345/p/9843001.html B:https://www.cnblogs.com/myx12345/p/9843021.html ...

  6. 汕头市赛srm1X T3

    给n<=100000个点的树,每个点有一个01串,长度m<=200,串的可以随时01取反,串的每一位对应权Vi,从根节点到某个节点经过决定哪些串取反后取得的最大价值为某个点的权值,求:在这 ...

  7. msp430入门编程26

    msp430中C语言开发工具应用 msp430入门学习 msp430入门编程

  8. OC-runtime 的温习

    -.runtime简介 runtime简称运行时,OC就是运行时机制,也就是运行时的一些机制,其中最主要的是消息机制: 对于C语言,函数的调用在编辑的时候,会决定调用哪个函数: 对于OC的函数,属于动 ...

  9. poj -1185 炮兵阵地 (经典状压dp)

    http://poj.org/problem?id=1185 参考博客:http://poj.org/problem?id=1185 大神博客已经讲的很清楚了,注意存状态的时候是从1开始的,所以初始化 ...

  10. java代码 猜数字小游戏

    import java.util.Scanner; import java.util.Random; public class mulTip{ public static void main(Stri ...