[USACO17DEC]Barn Painting (树形$dp$)】的更多相关文章

题目链接 Solution 比较简单的树形 \(dp\) . \(f[i][j]\) 代表 \(i\) 为根的子树 ,\(i\) 涂 \(j\) 号颜色的方案数. 转移很显然 : \[f[i][1]=\prod(f[t][2]+f[t][3])\] 其中 \(k\) 代表它的子节点. 其他两种颜色以此类推. 但需要注意的是对于颜色固定的点,除固定的颜色外,其他两种颜色的方案要赋为 \(0\) . PS : 要开 longlong ,以及还要 mod 1000000007 . Code #incl…
设\(f[i][j]\)为\(i\)子树,当\(i\)为\(j\)时的方案数 #include <bits/stdc++.h> using namespace std; #define int long long const int N = 1000005; const int mod = 1e+9+7; vector <int> g[N]; int vis[N],n,m,t1,t2,f[N][4],c[N]; void dfs(int p) { vis[p]=1; int fla…
题目描述 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 co…
数组越界那个RE+WA的姹紫嫣红的... 乘法原理求种类数,类似于没有上司的舞会. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define R(a,b,c) for(register int a = (b); a <= (c); ++ a) #define nR(a,b,c) for(regi…
https://codeforces.com/contest/1198/problem/D 原来是dp的思路,而且是每次切成两半向下递归.好像在哪里见过类似的,貌似是紫书的样子. 再想想好像就很显然的样子,并不会出现奇奇怪怪的合并的样子. #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int dp[51][51][51][51]; char g[51][51]; int main() { #if…
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3725 n个点排列,给每个点着色,求其中至少有m个红色的点连续的数目.f[i]表示前i个点至少有m个连续红色的个数,则f[i]=f[i-1]*2+2^(i-m-1)-f[i-m-1]. //STATUS:C++_AC_120MS_1784KB #include <functional> #include <algorithm> #include &l…
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5141 [算法] 树形DP 时间复杂度 : O(N) [代码] #include<bits/stdc++.h> using namespace std; typedef long long LL; ; ; ; struct edge { int to , nxt; } e[MAXN << ]; int n , k , tot; int color[MAXN] , hea…
http://codeforces.com/problemset/problem/300/D 题意:每一次操作可以选一个正方形,令边长为n,如果n为奇数那么可以从中间画一个十字,分成4个大小相等的边长为(n-1)/2的正方形.给一个正方形,求操作k次后能得到的不同图案的个数 思路:令f(s,k)表示边长为s的正方形操作k次后的答案总数,则f(s,k)=∑f(s/2,k1)*f(s/2,k2)*f(s/2,k3)*f(s/2,k4),其中s为奇数,k1+k2+k3+k4=k-1,令g(s,k)=Σ…
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了,整棵树的权值就只需要模拟即可,所以思路就转换为求哪一个点为根的权值最大. 这题需要用到一个二次扫描换根的思想,我们可以先从任意一个点去进行树形dp 并且得到从这个点开始去逐渐更新他的儿子节点 #include<cstdio> #include<cstring> #include<…
P2324 [SCOI2005]骑士精神 CF724B Batch Sort CF460C Present CF482A Diverse Permutation CF425A Sereja and Swaps CF1187C Vasya And Array P3419 [POI2005]SAM-Toy Cars CF196C Paint Tree *Loj #6560 小奇取石子 *CodeChef - CLPERM POJ2689. Prime Distance P1792 [国家集训队]种树…