[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…
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了,整棵树的权值就只需要模拟即可,所以思路就转换为求哪一个点为根的权值最大. 这题需要用到一个二次扫描换根的思想,我们可以先从任意一个点去进行树形dp 并且得到从这个点开始去逐渐更新他的儿子节点 #include<cstdio> #include<cstring> #include<…
数组越界那个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…
题目描述 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…
最近几天学了一下树形\(dp\) 其实早就学过了 来提高一下打开树形\(dp\)的姿势. 1.没有上司的晚会 我的人生第一道树形\(dp\),其实就是两种情况: \(dp[i][1]\)表示第i个人来时的最大人数 \(dp[i][0]\)表示第i个人不来时的最大人数 然后递归至叶子节点,倒推\(dp\) 状态转移方程: \[dp[root][1]+=dp[G[root][i]][0];\] \[dp[root][0]+=max(dp[G[root][i]][1],dp[G[root][i]][0…
Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows d…
C - 树形dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (…
Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get from any g…
题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.son) + 1,即剪掉与所有儿子(total(i.son))的边,还要剪掉与其父亲(+1)的边. dp[i][k] = min(dp[i][k],dp[i][j - k] + dp[i.son][k] - 2),即由i.son生成一个节点数为k的子树,再由i生成其他j-k个节点数的子树. 这里要还原i…