Luogu4084 [USACO17DEC]Barn Painting (树形DP)

Posted bingoyes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu4084 [USACO17DEC]Barn Painting (树形DP)相关的知识,希望对你有一定的参考价值。

数组越界那个RE+WA的姹紫嫣红的。。。
乘法原理求种类数,类似于没有上司的舞会。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios
    template<typename ATP>ios& operator >> (ATP &x)
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    
io;
using namespace std;

const int N = 100007;
const int mod = 1000000007;

struct Edge
    int nxt, pre;
e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v)
    e[++cntEdge] = (Edge)head[u], v, head[u] = cntEdge;


int col[N];
long long f[N][3];
inline void DFS(int u, int fa)
    if(col[u])
        f[u][col[u] - 1] = 1;
    else
        R(i,0,2)
            f[u][i] = 1;
    
    for(register int i = head[u]; i; i = e[i].nxt)
        int v = e[i].pre;
        if(v == fa) continue;
        DFS(v, u);
        f[u][0] = f[u][0] * (f[v][1] + f[v][2]) % mod;
        f[u][1] = f[u][1] * (f[v][0] + f[v][2]) % mod;
        f[u][2] = f[u][2] * (f[v][0] + f[v][1]) % mod;
    
    


int main()
//FileOpen();

    int n, K;
    io >> n >> K;
    R(i,2,n)
        int u, v;
        io >> u >> v;
        add(u, v);
        add(v, u);
    
    
    R(i,1,K)
        int x, nodeColor;
        io >> x >> nodeColor;
        col[x] = nodeColor;
    
    
    DFS(1, 0);
    
    printf("%lld", ((f[1][0] + f[1][1] + f[1][2]) % mod + mod) % mod);
    
    return 0;

以上是关于Luogu4084 [USACO17DEC]Barn Painting (树形DP)的主要内容,如果未能解决你的问题,请参考以下文章

Luogu4085 [USACO17DEC]Haybale Feast (线段树,单调队列)

[luogu P3065] [USACO12DEC]第一!First!

luogu P3110 [USACO14DEC]驮运Piggy Back

题解Luogu P3110 [USACO14DEC]驮运Piggy Back

[Luogu 3128] USACO15DEC Max Flow

luogu P2871 [USACO07DEC]手链Charm Bracelet