[USACO17DEC] Barn Painting
题目描述
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.
输入输出样例
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的更多相关文章
- [USACO17DEC]Barn Painting (树形$dp$)
题目链接 Solution 比较简单的树形 \(dp\) . \(f[i][j]\) 代表 \(i\) 为根的子树 ,\(i\) 涂 \(j\) 号颜色的方案数. 转移很显然 : \[f[i][1]= ...
- [USACO17DEC] Barn Painting - 树形dp
设\(f[i][j]\)为\(i\)子树,当\(i\)为\(j\)时的方案数 #include <bits/stdc++.h> using namespace std; #define i ...
- Luogu4084 [USACO17DEC]Barn Painting (树形DP)
数组越界那个RE+WA的姹紫嫣红的... 乘法原理求种类数,类似于没有上司的舞会. #include <iostream> #include <cstdio> #include ...
- [USACO 2017DEC] Barn Painting
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5141 [算法] 树形DP 时间复杂度 : O(N) [代码] #include< ...
- 我的刷题单(8/37)(dalao珂来享受切题的快感
P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and S ...
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- [学习笔记]树形dp
最近几天学了一下树形\(dp\) 其实早就学过了 来提高一下打开树形\(dp\)的姿势. 1.没有上司的晚会 我的人生第一道树形\(dp\),其实就是两种情况: \(dp[i][1]\)表示第i个人来 ...
- [USACO19FEB]Painting the Barn G
题意 \(n\)个矩阵\((0\le x_1,y_1,x_2,y_2\le 200)\),可交,可以再放最多两个矩阵(这两个矩阵彼此不交),使得恰好被覆盖\(k\)次的位置最大.\(n,k\le 10 ...
- 洛谷 P5542 [USACO19FEB]Painting The Barn
题目传送门 解题思路: 二维差分的板子题.题解传送门 AC代码: #include<iostream> #include<cstdio> using namespace std ...
随机推荐
- C++树的建立和遍历
#include<iostream.h> typedef char TElemtype; typedef struct Btree { TElemtype data; struct Btr ...
- GBDT(梯度提升树)scikit-klearn中的参数说明及简汇
1.GBDT(梯度提升树)概述: GBDT是集成学习Boosting家族的成员,区别于Adaboosting.adaboosting是利用前一次迭代弱学习器的误差率来更新训练集的权重,在对更新权重后的 ...
- Android 程序怎么打log
常见的做法: 1. 定义一个常量(变量)作为是否输出log的flag: 2. 定义一个常量(变量)作为log级别设定: 2. 调试.打包时,按需要调整常量的值,从而控制log打印. 常见代码参考: h ...
- Codeforces Round #388 (Div. 2) 749E(巧妙的概率dp思想)
题目大意 给定一个1到n的排列,然后随机选取一个区间,让这个区间内的数随机改变顺序,问这样的一次操作后,该排列的逆序数的期望是多少 首先,一个随机的长度为len的排列的逆序数是(len)*(len-1 ...
- 【距离GDKOI:44天&GDOI:107天】【BZOJ1040】[ZJOI2008] 骑士 (环套树DP)
其实已经准备退役了,但GDOI之前还是会继续学下去的!!当成兴趣在学,已经对竞赛失去信心了的样子,我还是回去跪跪文化课吧QAQ 第一道环套树DP...其实思想挺简单的,就把环拆开,分类处理.若拆成开的 ...
- Codeforces Round #428 (Div. 2) B
B. Game of the Rows(贪心) 题意: 有k种颜色,每种有\(a_i\)个,将这k种颜色放在一个\(n * 8格子里\) 放置规则不能出现两个不同颜色在相邻的格子里,相邻的定义为在同一 ...
- 2017 多校5 hdu 6093 Rikka with Number
2017 多校5 Rikka with Number(数学 + 数位dp) 题意: 统计\([L,R]\)内 有多少数字 满足在某个\(d(d>=2)\)进制下是\(d\)的全排列的 \(1 & ...
- 浅谈Visitor Pattern
第一步: 在介绍Visitor Pattern (访问者模式)之前,先简要介绍一下:双重分派. 在Visitor Pattern中双重分派是指:数据结构的每一个节点都可以接受一个访问者的调用(这句 ...
- 在Linux内核中添加系统调用,并编译内核
1 环境准备 运行系统:vmware下安装的ubuntu10.10 32bit桌面版. 编译内核版本: linux-2.6.32.63 内核目录: /home/wanchouchou/linuxKer ...
- react dva routerRedux 备忘
首先你需要import { Link, routerRedux } from 'dva/router'; 在方法里跳转用 function applyJobHandler(){ dispatch(ro ...