[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 ...
随机推荐
- pomelo环境配置(windows环境)
目录 简介 准备 安装 工程的创建 简介 1.网易开源,免费,业(diao)界(si)良(fu)心(li)呀,^.^ 2.游戏服务器框架(当然也可以用于web服务器) 3.高性能.高可伸缩.分布式,多 ...
- 2019 kali安装pip/pip3
新版的kali中内置的python没有安装pip 0x01 pip(python2)安装: https://bootstrap.pypa.io/2.6/get-pip.py python2 get-p ...
- ajax后台返回指定的错误码
js: $.ajax({ type: "POST", url: 'post.php', data: serialNumber + "&getSerialNumbe ...
- Linux 设置秘钥登录(SSH免密连接)
Secure Shell 协议,简称 SSH,是一种加密网络协议,用于客户端和主机之间的安全连接,并支持各种身份验证机制,目前最实用的身份验证机制就是基于密码的身份验证和基于公钥的身份验证两种.Lin ...
- jax-rs下载文件
@Path("/file") public class FileService { private static final String FILE_PATH = "c: ...
- 单源最短路问题--朴素Dijkstra & 堆优化Dijkstra
许久没有写博客,更新一下~ Dijkstra两种典型写法 1. 朴素Dijkstra 时间复杂度O(N^2) 适用:稠密图(点较少,分布密集) #include <cstdi ...
- HTTP 协议图解
HTTP 协议是一个非常重要的网络协议,我们平时能够使用浏览器浏览网页,其中一个非常重要的条件就是HTTP 协议. 0,什么是网络协议 互联网的目的是分享信息,网络协议是互联网的重要组成部分. 在互联 ...
- Pycharm中设置encoding
在Pycharm专业版中,为了防止文件在别的机器上出现乱码,所以需要进行字符编码的设置. 首先在Pycharm中的View中将下图中的Toolbar打上勾. 接着,工具栏就会出现,选中settings ...
- Javascript中的string类型使用UTF-16编码
2019独角兽企业重金招聘Python工程师标准>>> 在JavaScript中,所有的string类型(或者被称为DOMString)都是使用UTF-16编码的. MDN DOMS ...
- SpringCloud之整合Feign
假设提供者有如下服务接口方法 @RestController @RequestMapping("/person") public class PersonController { ...