题目描述

Farmer John has a large farm with NN barns (1 \le N \le 10^51≤N≤105 ), 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 NN 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 NN and KK (0 \le K \le N0≤K≤N ), respectively the number of barns on the farm and the number of barns that have already been painted.

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

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

输出格式:

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

输入输出样例

输入样例#1:

4 1
1 2
1 3
1 4
4 3 输出样例#1: 
8

树上dp求相邻节点不同的染色方案。
已染色的就把该节点的其他颜色的方案数置为0即可。
(这个难度评价有毒,把我骗进来了hhhh)
#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
#define pb push_back
using namespace std;
const int ha=1000000007;
vector<int> g[maxn];
int f[maxn][4],col[maxn];
int n,m,k,ans; inline int add(int x,int y){
x+=y;
if(x>=ha) return x-ha;
else return x;
} void dfs(int x,int fa){
f[x][1]=f[x][2]=f[x][3]=1; int to,tmp;
for(int i=g[x].size()-1;i>=0;i--){
to=g[x][i];
if(to==fa) continue; dfs(to,x); tmp=add(f[to][1],add(f[to][2],f[to][3]));
for(int j=1;j<=3;j++) f[x][j]=f[x][j]*(ll)add(tmp,ha-f[to][j])%ha;
} if(col[x]){
for(int i=1;i<=3;i++) if(i!=col[x]) f[x][i]=0;
}
} int main(){
int uu,vv;
scanf("%d%d",&n,&k);
for(int i=1;i<n;i++){
scanf("%d%d",&uu,&vv);
g[uu].pb(vv),g[vv].pb(uu);
}
for(int i=1;i<=k;i++){
scanf("%d%d",&uu,&vv);
col[uu]=vv;
} dfs(1,0); ans=add(add(f[1][2],f[1][1]),f[1][3]);
printf("%d\n",ans); return 0;
}

  


[USACO17DEC] Barn Painting的更多相关文章

  1. [USACO17DEC]Barn Painting (树形$dp$)

    题目链接 Solution 比较简单的树形 \(dp\) . \(f[i][j]\) 代表 \(i\) 为根的子树 ,\(i\) 涂 \(j\) 号颜色的方案数. 转移很显然 : \[f[i][1]= ...

  2. [USACO17DEC] Barn Painting - 树形dp

    设\(f[i][j]\)为\(i\)子树,当\(i\)为\(j\)时的方案数 #include <bits/stdc++.h> using namespace std; #define i ...

  3. Luogu4084 [USACO17DEC]Barn Painting (树形DP)

    数组越界那个RE+WA的姹紫嫣红的... 乘法原理求种类数,类似于没有上司的舞会. #include <iostream> #include <cstdio> #include ...

  4. [USACO 2017DEC] Barn Painting

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5141 [算法] 树形DP 时间复杂度 : O(N) [代码] #include< ...

  5. 我的刷题单(8/37)(dalao珂来享受切题的快感

    P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and S ...

  6. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  7. [学习笔记]树形dp

    最近几天学了一下树形\(dp\) 其实早就学过了 来提高一下打开树形\(dp\)的姿势. 1.没有上司的晚会 我的人生第一道树形\(dp\),其实就是两种情况: \(dp[i][1]\)表示第i个人来 ...

  8. [USACO19FEB]Painting the Barn G

    题意 \(n\)个矩阵\((0\le x_1,y_1,x_2,y_2\le 200)\),可交,可以再放最多两个矩阵(这两个矩阵彼此不交),使得恰好被覆盖\(k\)次的位置最大.\(n,k\le 10 ...

  9. 洛谷 P5542 [USACO19FEB]Painting The Barn

    题目传送门 解题思路: 二维差分的板子题.题解传送门 AC代码: #include<iostream> #include<cstdio> using namespace std ...

随机推荐

  1. hdu 1203 01背包 I need a offer

    hdu 1203  01背包  I need a offer 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:给你每个学校得到offe ...

  2. 【转】Unity3D Transform中有关旋转的属性和方法测试

    Transform有关旋转个属性和方法测试 一,属性 1,var eulerAngles : Vector3 public float yRotation = 5.0F; void Update()  ...

  3. 获取任意网站的图标,标题栏logo,网站logo

    https://www.hao123.com/favicon.ico      网站换成你想要的  大多数都可以

  4. vmware中linux虚拟机使用NAT模式不能连接外网解决

    linux虚拟机一直配置的桥接模式,今天改成NAT模式发现不能上外网 环境:VMware12,CentOS 6.8,NAT模式 ①电脑实际ip:192.168.1.100 ②NAT使用虚拟网卡网关: ...

  5. 【bzoj4146】[AMPPZ2014]Divisors 数论

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801411.html 题目描述 给定一个序列a[1],a[2],...,a[n].求满足i!=j且a[i]|a[j] ...

  6. 洛谷 P2155 [SDOI2008]沙拉公主的困惑 解题报告

    P2155 [SDOI2008]沙拉公主的困惑 题目描述 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为\(1\)到\(N\)的阶乘,但是,政府只发行编号与\(M!\ ...

  7. 【CZY选讲·最大子矩阵和】

    题目描述 有一个n*m的矩阵,恰好改变其中一个数变成给定的常数P,使得改变后的这个矩阵的最大子矩阵最大. 数据范围 n,m<=300. 题解:    ①如果没有p,那么二维矩阵和就是一维最长 ...

  8. Codeforces Round #324 (Div. 2) A

    A. Olesya and Rodion time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Python之面向对象:闭包和装饰器

    一.闭包 1. 如果一个函数定义在另一个函数的作用域内,并且引用了外层函数的变量,则该函数称为闭包. def outter(): name='python' def inner(): print na ...

  10. Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2)

    Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2)     最近在做控制算法实现的时候,对于其中参杂的各种差分.卷积很头疼,就在网上搜集了些资料,汇总于此,以做备 ...