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

[USACO 2017DEC] Barn Painting

时间:2018-10-16 19:56:38      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:printf   const   color   pen   date   bit   char   amp   nbsp   

[题目链接]

         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 + 10;
const int MAXC = 5; 
const int P = 1e9 + 7;

struct edge
{
    int to , nxt;
} e[MAXN << 1];

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 = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == -) f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 0;
    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] != -1) return f[u][k];
    f[u][k] = 1;
    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] = 0;
            else mul(f[u][k] , dp(v , color[v] , u));
        } else
        {
            LL value = 0;
            for (int j = 1; j <= 3; 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 = 1; i < n; i++)
    {
        int x , y;
        read(x); read(y);
        addedge(x , y);
        addedge(y , x);
    }
    for (int i = 1; i <= k; i++)
    {
        int b , c;
        read(b); read(c);
        color[b] = c;        
    }
    for (int i = 1; i <= n; i++) f[i][1] = f[i][2] = f[i][3] = -1;
    if (color[1]) 
    {
        printf("%lld\n",dp(1 , color[1] , -1));
    } else
    {
        LL ans = 0;
        for (int i = 1; i <= 3; i++) update(ans , dp(1 , i , -1));
        printf("%lld\n",ans);
    }
    
    return 0;
}

 

[USACO 2017DEC] Barn Painting

标签:printf   const   color   pen   date   bit   char   amp   nbsp   

原文地址:https://www.cnblogs.com/evenbao/p/9800139.html

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