[USACO17DEC] Barn Painting G(树形dp)

[USACO17DEC] Barn Painting G

题面翻译

题意:给定一颗 N N N 个节点组成的树,你要给每个点涂上三种颜色之一,其中有 K K K 个节点已染色,要求任意两相邻节点颜色不同,求合法染色方案数。答案对 1 0 9 + 7 10^9+7 109+7 取模。

题目描述

Farmer John has a large farm with N N N barns ( 1 ≤ N ≤ 1 0 5 1 \le N \le 10^5 1N105), some of which are already painted and some not yet painted. Farmer John wants to paint these remaining barns so that all the barns are painted, but he only has three paint colors available. Moreover, his prize cow Bessie becomes confused if two barns that are directly reachable from one another are the same color, so he wants to make sure this situation does not happen.

It is guaranteed that the connections between the N N N barns do not form any ‘cycles’. That is, between any two barns, there is at most one sequence of connections that will lead from one to the other.

How many ways can Farmer John paint the remaining yet-uncolored barns?

输入格式

The first line contains two integers N N N and K K K ( 0 ≤ K ≤ N 0 \le K \le N 0KN), respectively the number of barns on the farm and the number of barns that have already been painted.

The next N − 1 N-1 N1 lines each contain two integers x x x and y y y ( 1 ≤ x , y ≤ N , x ≠ y 1 \le x, y \le N, x \neq y 1x,yN,x=y) describing a path directly connecting barns x x x and y y y.

The next K K K lines each contain two integers b b b and c c c ( 1 ≤ b ≤ N 1 \le b \le N 1bN, 1 ≤ c ≤ 3 1 \le c \le 3 1c3) indicating that barn b b b is painted with color c c c.

输出格式

Compute the number of valid ways to paint the remaining barns, modulo 1 0 9 + 7 10^9 + 7 109+7, such that no two barns which are directly connected are the same color.

样例 #1

样例输入 #1

4 1
1 2
1 3
1 4
4 3

样例输出 #1

8

思路

这道题不就是跟那个没有上司的舞会那道题类似吗?只不过这边有3中状态。具体的状态推到可以看:
在这里插入图片描述
图片来源,讲的很详细

代码

//本道题跟上司的舞会很像,3种颜色

#include<iostream>
#include<algorithm>
#include<cstring>

#define int long long

using namespace std;

const int N = 2e5+10,mod = 1e9+7,M = 2*N;

int f[N][4];//j表示3种颜色
int e[M],ne[M],w[M],h[N],idx;
bool st[N];
int color[N];
int n,k;

void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int u,int fa){
    if(color[u]){
        //如果已经上过色了,其他两种颜色的方案数为0。
        f[u][color[u]]=1;
    }else{
        f[u][1]=f[u][2]=f[u][3]=1;
    }
    
    for(int i=h[u];~i;i=ne[i]){
        int j=e[i];
        
        if(j==fa)continue;
        
        dfs(j,u);
        //因为这个是分步进行的,所以要乘法
        f[u][1]=f[u][1]*(f[j][2]+f[j][3])%mod;
        f[u][2]=f[u][2]*(f[j][1]+f[j][3])%mod;
        f[u][3]=f[u][3]*(f[j][1]+f[j][2])%mod;
    }
}

signed main(){
    cin>>n>>k;
    
    memset(h,-1,sizeof h);
    
    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        add(a,b);
        add(b,a);
    }
    
    for(int i=1;i<=k;i++){
        int x,y;
        cin>>x>>y;
        color[x]=y;
    }
    
    dfs(1,-1);
    
    cout<<(f[1][1]+f[1][2]+f[1][3])%mod;
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值