码迷,mamicode.com
首页 > 其他好文 > 详细

[codeforces-543-D div1]树型DP

时间:2015-05-27 22:28:42      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

题意:给一棵树的边标上0或1,求以节点i为源点,其它点到i的唯一路径上的1的边数不超过1条的方案数,输出所有i的答案。

思路:令f[i]表示以节点i为源点,只考虑子树i时的方案数,ans[i]为最后答案,fa[i]为i的父亲,则不难得出以下转移方程:

f[i] = ∏(1 + f[v]),v是i的儿子      ans[i] = f[i] * (1 + ans[fa[i]] / (1 + f[i]))

由于除法取模运算的存在,不得不对1+f[i]求逆元,但1+f[i]可能等于MOD,于是这种情况下结果就是错的了,不能用这个公式求。

令g[i] = ans[fa[i]] / (1 + f[i]),注意到g[i]实际上等于∏(1 + f[v]) * g[fa[i]],v是i的兄弟,于是可以增加一个前缀积数组和一个后缀积数组用来得到∏(1 + f[v]),就不难得到g[i]和最后的答案了。

技术分享
  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21 
 22 using namespace std;
 23 
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define define_m int m = (l + r) >> 1
 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 33 #define all(a) (a).begin(), (a).end()
 34 #define lowbit(x) ((x) & (-(x)))
 35 #define constructInt5(name, a, b, c, d, e) name(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0): a(a), b(b), c(c), d(d), e(e) {}
 36 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 37 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 38 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 39 #define pchr(a) putchar(a)
 40 #define pstr(a) printf("%s", a)
 41 #define sstr(a) scanf("%s", a)
 42 #define sint(a) scanf("%d", &a)
 43 #define sint2(a, b) scanf("%d%d", &a, &b)
 44 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 45 #define pint(a) printf("%d\n", a)
 46 #define test_print1(a) cout << "var1 = " << a << endl
 47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 49 #define mp(a, b) make_pair(a, b)
 50 #define pb(a) push_back(a)
 51 
 52 typedef long long LL;
 53 typedef pair<int, int> pii;
 54 typedef vector<int> vi;
 55 
 56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 58 const int maxn = 2e5 + 7;
 59 const int md = 1e9 + 7;
 60 const int inf = 1e9 + 7;
 61 const LL inf_L = 1e18 + 7;
 62 const double pi = acos(-1.0);
 63 const double eps = 1e-6;
 64 
 65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 68 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 70 int make_id(int x, int y, int n) { return x * n + y; }
 71 
 72 struct Graph {
 73     vector<vector<int> > G;
 74     void clear() { G.clear(); }
 75     void resize(int n) { G.resize(n + 2); }
 76     void add(int u, int v) { G[u].push_back(v); }
 77     vector<int> & operator [] (int u) { return G[u]; }
 78 };
 79 Graph G, pre, suf;
 80 int fa[maxn], f[maxn], ans[maxn], id[maxn], g[maxn];
 81 
 82 void dfs(int n) {
 83     f[n] = 1;
 84     int sz = G[n].size();
 85     rep_up0(i, sz) {
 86         int v = G[n][i];
 87         dfs(v);
 88         f[n] = (LL)f[n] * (1 + f[v]) % md;
 89     }
 90 }
 91 
 92 void getAns(int n) {
 93     int sz = G[n].size();
 94     int fn = fa[n], in = id[n];
 95     pre[n].resize(sz + 2);
 96     suf[n].resize(sz + 2);
 97     pre[n][0] = suf[n][sz + 1] = 1;
 98     if (n == 1) {
 99         ans[n] = f[n];
100         g[n] = 1;
101     }
102     else {
103         g[n] = (1 + (LL)pre[fn][in - 1] % md * suf[fn][in + 1] % md * g[fn]) % md;
104         ans[n] = (LL)f[n] * g[n] % md;
105     }
106     rep_up0(i, sz) {
107         int v = G[n][i];
108         pre[n][i + 1] = (LL)pre[n][i] * (1 + f[v]) % md;
109     }
110     rep_down0(i, sz) {
111         int v = G[n][i];
112         suf[n][i + 1] = (LL)suf[n][i + 2] * (1 + f[v])% md;
113     }
114     rep_up0(i, sz) {
115         int v = G[n][i];
116         getAns(v);
117     }
118 }
119 
120 int main() {
121     //freopen("in.txt", "r", stdin);
122     int n;
123     cin >> n;
124     G.resize(n);
125     pre.resize(n);
126     suf.resize(n);
127     for (int i = 2; i <= n; i ++) {
128         int x;
129         sint(x);
130         G.add(x, i);
131         fa[i] = x;
132         id[i] = G[x].size();
133     }
134     dfs(1);
135     getAns(1);
136     rep_up1(i, n) printf("%d%c", ans[i], i == n? \n :  );
137     return 0;
138 }
View Code

 

[codeforces-543-D div1]树型DP

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4534517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!