[Question ]:
An undirected graph is given. Each vertex in the graph has a color (1 or 0). Ask how many different cutting methods are there to make each area have a black spot.
[Knowledge point ]:
Tree DP
[Question ]:
The DP equation is declared as follows:
DP [u] [0]: indicates the number of cutting methods for a tree with the same node as U, the region where u is located is the only region without black spots.
DP [u] [1]: indicates the number of cutting methods for the tree with the same node as U, so that each area after cutting (including the area where u is located) has a black spot.
When the U point is a Black Point
DP [u] [0] = 0. DP [u] [1] = product of several (DP [v] [0] + dp [v] [1.
When u is a white Node
DP [u] [0] = product of several (DP [v] [0] + dp [v] [1. DP [u] [1] = several (DP [v] [1] and other subnodes (DP [v] [0] + dp [v] [1]).
The description is not very clear and needs to be improved, because my ideas have been vague.
[Code ]:
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #include <queue> 6 #include <stack> 7 #include <cstdio> 8 #include <string> 9 #include <vector>10 #include <cstring>11 #include <sstream>12 #include <iostream>13 #include <algorithm>14 #include <bitset>15 #include <climits>16 using namespace std;17 18 #define wh while19 #define inf (int)(~0u/2)20 #define FOR(i, n) for(int i = 0; i < n; i++)21 #define FOR1(i, n) for(int i = 1; i < n; i++)22 #define FOR2(i, n) for(int i = 0; i <= n; i++)23 #define REP(i,n) for(int i = 1; i <= n; i++)24 #define FORI(it,n) for(typeof(n.begin()) it = n.begin(); it != n.end(); it++)25 #define sf scanf26 #define pf printf27 #define frs first28 #define sec second29 #define psh push_back30 #define mkp make_pair31 #define PB(x) push_back(x)32 #define MP(x, y) make_pair(x, y)33 #define clr(abc,z) memset(abc,z,sizeof(abc))34 #define lt(v) v << 135 #define rt(v) v << 1 | 136 //#define mid ((l + r) >> 1)37 #define lson l, mid, v << 138 #define rson mid + 1, r, v << 1 | 139 40 #define fre freopen("1.txt", "r", stdin)41 42 typedef long long LL;43 typedef long double LD;44 45 const int MOD = 1e9 + 7;46 47 const int maxn = 1e5 + 100;48 int dp[maxn][2];49 int val[maxn];50 vector<int> G[maxn];51 int add(int a, int b){52 return (a + b) % MOD;53 }54 int mul(int a, int b){55 return a * (LL)b % MOD;56 }57 void dfs(int u){58 dp[u][val[u]] = 1; dp[u][val[u] ^ 1] = 0;59 FOR(i, (int)G[u].size()){60 int v = G[u][i];61 dfs(v);62 int last0 = dp[u][0], last1 = dp[u][1];63 dp[u][0] = mul(last0, add(dp[v][0], dp[v][1]));64 dp[u][1] = add(mul(last0, dp[v][1]),65 mul(last1, add(dp[v][0], dp[v][1])));66 }67 }68 69 int main(){70 int n;71 wh(sf("%d", &n) != EOF){72 FOR(i, n) G[i].clear();73 FOR(i, n - 1){74 int tmp; sf("%d", &tmp);75 G[tmp].PB(i + 1);76 }77 FOR(i, n) sf("%d", &val[i]);78 dfs(0);79 pf("%d\n", dp[0][1]);80 }81 }View code
Cf263 (D1) B tree DP