题目链接

Solution

比较简单的树形 \(dp\) 。

\(f[i][j]\) 代表 \(i\) 为根的子树 ,\(i\) 涂 \(j\) 号颜色的方案数。

转移很显然 :

\[f[i][1]=\prod(f[t][2]+f[t][3])
\]

其中 \(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$)的更多相关文章

  1. [USACO17DEC] Barn Painting - 树形dp

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

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

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

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

  4. Codeforces - 1198D - Rectangle Painting 1 - dp

    https://codeforces.com/contest/1198/problem/D 原来是dp的思路,而且是每次切成两半向下递归.好像在哪里见过类似的,貌似是紫书的样子. 再想想好像就很显然的 ...

  5. ZOJ-3725 Painting Storages DP

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3725 n个点排列,给每个点着色,求其中至少有m个红色的点连续的数 ...

  6. [USACO 2017DEC] Barn Painting

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5141 [算法] 树形DP 时间复杂度 : O(N) [代码] #include< ...

  7. [CodeForces 300D Painting Square]DP

    http://codeforces.com/problemset/problem/300/D 题意:每一次操作可以选一个正方形,令边长为n,如果n为奇数那么可以从中间画一个十字,分成4个大小相等的边长 ...

  8. Educational Codeforces Round 67 E.Tree Painting (树形dp)

    题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...

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

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

随机推荐

  1. Java各类型占字节数

    byte 1字节short 2字节int 4字节long 8字节float 4字节double 8字节char 2字节boolean 1字节 其中,换算关系: 1GB=1024MB 1MB=1024K ...

  2. pyAudioAnalysis-audioFeatureExtraction 错误纠正

    1. TypeError: mfccInitFilterBanks() takes 2 positional arguments but 7 were given The issue In the f ...

  3. 关于NewJson dll 引用不一致

    {System.IO.FileLoadException: 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKe ...

  4. python工程的结构

    1 python系统库的位置 大部分系统库在/usr/lib64/python2.7目录下,但是像sys模块,是python内置的库,是用c实现的,直接连接进了python.exe中了. 也就是说,在 ...

  5. C++ 14 新特性总结

    转载自: http://www.codeceo.com/article/cpp-14-new-features.html C++14 这一继C++11 之后的新的 C++ 标准已经被正式批准,正在向 ...

  6. 7、 正则化(Regularization)

    7.1 过拟合的问题 到现在为止,我们已经学习了几种不同的学习算法,包括线性回归和逻辑回归,它们能够有效地解决许多问题,但是当将它们应用到某些特定的机器学习应用时,会遇到过拟合(over-fittin ...

  7. VirtualStringTree常用类和属性

    重要的类:TBaseVirtualTree = class(TCustomControl)TCustomVirtualStringTree = class(TBaseVirtualTree)TVirt ...

  8. HDOJ 1150 Machine Schedule

    版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss https://blog.csdn.net/u012797220/article/details/3 ...

  9. Html5移动端页面自适应布局详解(rem布局)

    在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport,通读网上的各种对于viewport的解释之后 大概viewport可以理解为三种 1.layout viewport  ...

  10. Could not load file or assembly system.data.sqlite.dll or one for it's depedencies

    最近做一个winform项目,因为数据库用的表很少,所以用的是轻量级数据库sqlite.sqlite的优点很多,但是他要分两个版本,32位或者64位,不能同时兼容. 我遇到的问题是,我在开发端用的是. ...