BZOJ 5141: [Usaco2017 Dec]Barn Painting

Description

Farmer John has a large farm with N barns (1≤N≤10^5), some of which are already painted and some n

ot 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 thi

s situation does not happen.It is guaranteed that the connections between the N barns do not form an

y 'cycles'. That is, between any two barns, there is at most one sequence of connections that will l

ead from one to the other.How many ways can Farmer John paint the remaining yet-uncolored barns?

给你一颗大小为n<=1e5的树,三种颜色,每个点涂一种颜色,相邻点不能同色。下面告诉你一些点已经被涂色,并

且告诉你是哪一种颜色,问你涂完整棵树有多少种方法?mod 1e9+7。

 

Input

The first line contains two integers N and K (0≤K≤N), respectively the number of barns on the farm 

and the number of barns that have already been painted.

The next N-1lines each contain two integers xx and yy (1≤x,y≤N,x≠y) describing a path directly connecting barns x and y.

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

 

Output

Compute the number of valid ways to paint the remaining barns, modulo 10^9+7, 

such that no two barns which are directly connected are the same color.

 

Sample Input

4 1
1 2
1 3
1 4
4 3

Sample Output

8

 

题目链接

 

又是一道树形dp的题

这道题和没有上司的舞会还是挺像的,这个可能更加复杂一些。不过我觉得难度可能都还属于树形dp的入门题。

树形dp本质上来说就是dfs一遍树然后进行递推,这道题中可以开个二维的数组dp[i][j]表示编号为i的点涂上j颜色的方案数。

显然每个点的方案数和它的子树是有关的,所以我们要先遍历到叶子节点然后一步步往回推。

由于相邻的点颜色不能相同,所以转移的时候只能由颜色不同的转移过来。且当前点的方案数应为其子树方案数的乘积。打个比方,对于点i来说,它有两个子树j,k。那么转移方程应为

dp[i][1]*=(dp[j][2]+dp[j][3])*(dp[k][2]+dp[k][3]);

dp[i][2]*=(dp[j][1]+dp[j][3])*(dp[k][1]+dp[k][3]);

dp[i][3]*=(dp[j][1]+dp[j][2])*(dp[k][1]+dp[k][2]);

至于为什么是*=,这就涉及到了dp初始值的设立。

对于一个最初没有涂色的点i,很显然刚遍历到的时候我们设置点i的初始值 dp[i][1]=dp[i][2]=dp[i][3]=1;

而如果点i已经被上色了,那么只有被上的颜色的数组才能为1(dp[i][col[i]]=1),而其他2个都为0。

所以之前用*=来更新保证了如果当前点已经被上色了的话,上其他颜色的方案数为0。

然后随便选个点i为根跑一遍dfs输出dp[i][1]+dp[i][2]+dp[i][3]就好了

#include<cstdio>
#include<iostream>
#include<cstring>
#define p 1000000007
#define LL long long
using namespace std;
inline int read()
{
    int sum=0;
    char ch =getchar();
    while(ch<'0'||ch>'9')
        ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        sum=sum*10+ch-'0';
        ch=getchar();
    }
    return sum;
}
int n,m,tot=0;
int Head[100005],col[100005];//Head用于邻接表  col记录颜色
LL dp[100005][4];
bool visit[100005];//由于存图为无向图,所以要记录下已经走过的点防止死循环
struct tree
{
    int next,node;
}h[200010];
inline void add(int u,int v)//邻接表存图
{
    h[++tot].next=Head[u];
    h[tot].node=v;
    Head[u]=tot;
}
void dfs(int pos)//dfs遍历
{
    visit[pos]=1;
    if(col[pos])//如果已经上过色了,其他两种颜色的方案数为0。
        dp[pos][col[pos]]=1;
    else//三种颜色都可以被♂上
    {
        dp[pos][1]=1;
        dp[pos][2]=1;
        dp[pos][3]=1;
    }
    for(register int i=Head[pos];i;i=h[i].next)//找到当前点所有的子节点
    {
        int v=h[i].node;
        if(!visit[v])
        {
            dfs(v);//一直向下遍历直到叶子节点返回
            dp[pos][1]=dp[pos][1]*((dp[v][2]+dp[v][3])%p)%p;
            dp[pos][2]=dp[pos][2]*((dp[v][1]+dp[v][3])%p)%p;
            dp[pos][3]=dp[pos][3]*((dp[v][2]+dp[v][1])%p)%p;//转移  记得取模!
        }
    }
}
int main()
{
    int x,y;
    n=read();
    m=read();
    for(register int i=1;i<n;++i)
    {
        x=read();
        y=read();
        add(x,y);
        add(y,x);//加边
    }
    for(register int i=1;i<=m;++i)
    {
        x=read();
        y=read();
        col[x]=y;//记录颜色
    }
    dfs(1);//随便把一个点当根就好了
    cout<<(dp[1][1]+dp[1][2]+dp[1][3])%p<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值