码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu4084 [USACO17DEC]Barn Painting (树形DP)

时间:2019-07-26 21:11:27      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:mem   --   define   type   print   tchar   for   system   return   

数组越界那个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)

标签:mem   --   define   type   print   tchar   for   system   return   

原文地址:https://www.cnblogs.com/bingoyes/p/11252822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!