[codeforces-543-D div1]树型DP
题意:给一棵树的边标上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]和最后的答案了。
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#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) {}
#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) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 2e5 + ;
const int md = 1e9 + ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } struct Graph {
vector<vector<int> > G;
void clear() { G.clear(); }
void resize(int n) { G.resize(n + ); }
void add(int u, int v) { G[u].push_back(v); }
vector<int> & operator [] (int u) { return G[u]; }
};
Graph G, pre, suf;
int fa[maxn], f[maxn], ans[maxn], id[maxn], g[maxn]; void dfs(int n) {
f[n] = ;
int sz = G[n].size();
rep_up0(i, sz) {
int v = G[n][i];
dfs(v);
f[n] = (LL)f[n] * ( + f[v]) % md;
}
} void getAns(int n) {
int sz = G[n].size();
int fn = fa[n], in = id[n];
pre[n].resize(sz + );
suf[n].resize(sz + );
pre[n][] = suf[n][sz + ] = ;
if (n == ) {
ans[n] = f[n];
g[n] = ;
}
else {
g[n] = ( + (LL)pre[fn][in - ] % md * suf[fn][in + ] % md * g[fn]) % md;
ans[n] = (LL)f[n] * g[n] % md;
}
rep_up0(i, sz) {
int v = G[n][i];
pre[n][i + ] = (LL)pre[n][i] * ( + f[v]) % md;
}
rep_down0(i, sz) {
int v = G[n][i];
suf[n][i + ] = (LL)suf[n][i + ] * ( + f[v])% md;
}
rep_up0(i, sz) {
int v = G[n][i];
getAns(v);
}
} int main() {
//freopen("in.txt", "r", stdin);
int n;
cin >> n;
G.resize(n);
pre.resize(n);
suf.resize(n);
for (int i = ; i <= n; i ++) {
int x;
sint(x);
G.add(x, i);
fa[i] = x;
id[i] = G[x].size();
}
dfs();
getAns();
rep_up1(i, n) printf("%d%c", ans[i], i == n? '\n' : ' ');
return ;
}
[codeforces-543-D div1]树型DP的更多相关文章
- Codeforces 23E Tree(树型DP)
题目链接 Tree $dp[x][i]$表示以x为根的子树中x所属的连通快大小为i的时候 答案最大值 用$dp[x][j]$ * $dp[y][k]$ 来更新$dp[x][j + k]$. (听高手说 ...
- Codeforces 581F Zublicanes and Mumocrates(树型DP)
题目链接 Round 322 Problem F 题意 给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...
- Codeforces 149D Coloring Brackets(树型DP)
题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- POJ3659 Cell Phone Network(树上最小支配集:树型DP)
题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...
- POJ 3342 - Party at Hali-Bula 树型DP+最优解唯一性判断
好久没写树型dp了...以前都是先找到叶子节点.用队列维护来做的...这次学着vector动态数组+DFS回朔的方法..感觉思路更加的清晰... 关于题目的第一问...能邀请到的最多人数..so ea ...
- 【XSY1905】【XSY2761】新访问计划 二分 树型DP
题目描述 给你一棵树,你要从\(1\)号点出发,经过这棵树的每条边至少一次,最后回到\(1\)号点,经过一条边要花费\(w_i\)的时间. 你还可以乘车,从一个点取另一个点,需要花费\(c\)的时间. ...
- 洛谷P3354 Riv河流 [IOI2005] 树型dp
正解:树型dp 解题报告: 传送门! 简要题意:有棵树,每个节点有个权值w,要求选k个节点,最大化∑dis*w,其中如果某个节点到根的路径上选了别的节点,dis指的是到达那个节点的距离 首先这个一看就 ...
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
随机推荐
- Xss Game挑战
前言 最新学习了下xss的更深入的东西,学习了一波浏览器解析机制和XSS向量编码的知识. 这里就些xss的练习题巩固知识 学习的话结合如下两篇文章看,从例子和基础原理层面都有: http://boba ...
- 从零开始学AB测试:躲坑篇
AB测试的原理很简单,只用到了最简单的统计假设检验,但表面的简单通常都隐藏着陷阱,这一点没有经过实践的摸爬滚打是不容易看到的,今天我就把前人已经踩过的坑,一共15个,给大家分享一下.在分享之前,大家脑 ...
- redis: List列表类型(四)
list设置值(头部):lpush list one list设置值(尾部):**rpush ** list one list获取值:lrange list 0 -1 list获取指定范围的值:lra ...
- redis:String字符串类型(三)
字符串拼接(如果key不存在则创建):append name " applesnt" 获取字符串的长度:strlen name 127.0.0.1:6379> set nam ...
- LeetCode466. Count The Repetitions
题目链接 传送门 题意 定义一个特殊的串, 现在给出串S1和S2的参数, 问: S2最多可以多少个连接起来扔是S1的子序列, 求出这个最大值 解题思路 注意s1与S1的区别, 可以去看题目描述, 预处 ...
- 使用STM8S i2c对TPS65987寄存器进行读写
上图是TPS65987的i2c读写协议,和标准i2c协议有点出入,不过也不难理解,在读的时候i2c slave在发送数据过来之前会先发送1byte数据表示后面会有几个字节数据过来,在写的时候i2c h ...
- 小白也能轻松上手的Prometheus教程
这篇文章将承接此前关于使用Prometheus配置自定义告警规则的文章.在本文中,我们将demo安装Prometheus的过程以及配置Alertmanager,使其能够在触发告警时能发送邮件,但我们将 ...
- html+css的用户注册界面
注册界面样图 代码实现 html部分 <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- UVA10599:Robots(II)(最长上升子序列)
Your company provides robots that can be used to pick up litter from fields after sporting events an ...
- APP路由还能这样玩
本文主要讲述一种设计思路,组件化架构市面上已经有很多大厂成熟的方案,但是在组件化过程中,偶尔会遇到2个独立业务子模块间没有相互引用,也需要能直接调用对方的功能,因此我想到通过方法路由来解决,如果还有疑 ...