CF1111E Tree 树链剖分,DP
CF1111E Tree
过年了,洛咕还没爬这次的题,先放个CF的链接吧。
补个LG传送门。
对于每个询问点\(x\),设它的祖先即不能和它放在同一个集合中的点的个数为\(f[x]\),设\(dp[i][j]\)表示前\(i\)个询问点放在\(j\)个非空集合中的方案数,注意这里“前\(i\)个”的意义,这表示会对第\(i\)个点造成影响的点都已被考虑过了,转移就是\(dp[i][j] = dp[i - 1][j] * (j - f[j]) + dp[i -1][j - 1]\)。
下面的问题就是怎么处理出\(f\)数组和找出DP的顺序。发现\(f\)数组可以直接树剖:先在线段树上把所有询问点更新一遍,然后再查询每个点到当前根的路径上询问点的个数,\(f[x]\)就是线段树上查询的值\(- 1\)(不算自己)。处理出\(f\)数组之后,发现祖先的\(f\)值一定比子孙的\(f\)值小,那么直接对\(f\)数组排一边序就可以DP了。
//written by newbiechd
#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
#define R register
#define I inline
#define B 1000000
#define L long long
using namespace std;
const int N = 100003, yyb = 1e9 + 7;
char buf[B], *p1, *p2;
I char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, B, stdin), p1 == p2) ? EOF : *p1++; }
I int rd() {
R int f = 0;
R char c = gc();
while (c < 48 || c > 57)
c = gc();
while (c > 47 && c < 58)
f = f * 10 + (c ^ 48), c = gc();
return f;
}
int a[N], s[N], fa[N], dep[N], siz[N], son[N], dfn[N], top[N], f[N], v[N << 2], n, tim;
L dp[N], ans;
vector <int> g[N];
void dfs1(int x, int f) {
fa[x] = f, dep[x] = dep[f] + 1, siz[x] = 1;
for (R int i = 0, y, m = 0; i < s[x]; ++i)
if ((y = g[x][i]) ^ f) {
dfs1(y, x), siz[x] += siz[y];
if (siz[y] > m)
m = siz[y], son[x] = y;
}
}
void dfs2(int x, int t) {
dfn[x] = ++tim, top[x] = t;
if (son[x])
dfs2(son[x], t);
for (R int i = 0, y; i < s[x]; ++i)
if ((y = g[x][i]) ^ fa[x] && y ^ son[x])
dfs2(y, y);
}
void modify(int k, int l, int r, int x, int y) {
v[k] += y;
if (l == r)
return ;
R int p = k << 1, q = p | 1, m = l + r >> 1;
if (x <= m)
modify(p, l, m, x, y);
else
modify(q, m + 1, r, x, y);
}
int tquery(int k, int l, int r, int x, int y) {
if (x <= l && r <= y)
return v[k];
R int p = k << 1, q = p | 1, m = l + r >> 1, o = 0;
if (x <= m)
o += tquery(p, l, m, x, y);
if (m < y)
o += tquery(q, m + 1, r, x, y);
return o;
}
int query(int x, int y) {
R int o = 0;
while (top[x] ^ top[y]) {
if (dep[top[x]] < dep[top[y]])
swap(x, y);
o += tquery(1, 1, n, dfn[top[x]], dfn[x]), x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
return o + tquery(1, 1, n, dfn[x], dfn[y]);
}
int main() {
R int Q, k, m, rt, i, j, x, y, flag;
n = rd(), Q = rd();
for (i = 1; i < n; ++i)
x = rd(), y = rd(), g[x].push_back(y), g[y].push_back(x);
for (i = 1; i <= n; ++i)
s[i] = g[i].size();
dfs1(1, 0), dfs2(1, 1);
while (Q--) {
k = rd(), m = rd(), rt = rd(), ans = 0, flag = 0;
for (i = 1; i <= k; ++i)
a[i] = rd(), modify(1, 1, n, dfn[a[i]], 1);
for (i = 1; i <= k; ++i) {
f[i] = query(a[i], rt) - 1;
if (f[i] >= m)
flag = 1;
}
for (i = 1; i <= k; ++i)
modify(1, 1, n, dfn[a[i]], -1), dp[i] = 0;
if (flag) {
printf("0\n");
continue;
}
sort(f + 1, f + k + 1), dp[0] = 1;
for (i = 1; i <= k; ++i)
for (j = min(i, m); ~j; --j) {
if (j <= f[i])
dp[j] = 0;
dp[j] = (dp[j] * (j - f[i]) + dp[j - 1]) % yyb;
}
for (j = 1; j <= k; ++j)
ans = (ans + dp[j]) % yyb;
printf("%I64d\n", ans);
}
return 0;
}
CF1111E Tree 树链剖分,DP的更多相关文章
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- 4.12 省选模拟赛 LCA on tree 树链剖分 树状数组 分析答案变化量
LINK:duoxiao OJ LCA on Tree 题目: 一道树链剖分+树状数组的神题. (直接nQ的暴力有50. 其实对于树随机的时候不难想到一个算法 对于x的修改 暴力修改到根. 对于儿子的 ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- poj 3237 Tree 树链剖分
题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- poj 3237 Tree 树链剖分+线段树
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
随机推荐
- SpringBoot整合Redis初实践
Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理. 有时,为了提升整个网站的性能,在开发时会将经常访问的数据进行缓存,这样在调用这个数据接口时,可以提 ...
- 使用MVC Razor生成格式良好的HTML Body作为邮件内容
PS: 实例化邮件类MailMessage有一个属性IsBodyHtml指示邮件正文是否是HTML格式. 我想利用Razor View的Model Binding / Rendering功能为我从AS ...
- November 17th 2016 Week 47th Thursday
Don't cry because it is over. Smile because it happened. 不要因为结束而哭泣:微笑吧,因为我们曾经拥有. My ex-girlfriend ha ...
- ubuntu下以16进制形式查看class文件、反编译class文件
参考:https://blog.csdn.net/xyh930929/article/details/83860464 1.vim HelloWorld.class 2.输入英文状态下的冒号:,然后输 ...
- 切换composer国内镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
- Payment:微信支付配置文件设置说明
项目GitHub地址:https://github.com/helei112g/payment 微信支付个人觉得在帐号设置上有些麻烦,帐号太多啦,支付宝目前就是一个应用基本涵盖了所有的接口开发. 可能 ...
- RAC with asm on AIX, ORA-01114 error,with "gipcretAuthFail (22) " in ocssd.log
I/O Errors in Alert log with ORA-29701, with "gipcWait failed with 16" in trace (文档 ID 149 ...
- this 的使用方法 —— javascript中的this讲解!
从自己刚刚开始学习javascript到现在已经很久了,今天得益于新酱的细心讲解,总算是把this这个“雾中花”看清晰了. 在此首先感谢新酱的讲解 下面将this的一些基本使用和大家分享一下: 查看t ...
- Js 中的事件委托/事件代理
什么叫事件委托/事件代理呢 ? JavaScript高级程序设计上讲:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. 事件冒泡: 当事件发生后,这个事件就要开始传 ...
- Linux无法识别Broadcom 802.11abgn无线网卡
折腾了好久,都无法解决 索性后来直接使用的usb外置网卡,勉强能用(使用极其不便) 最后使尽浑身解数,冲着萤火般的希望,好在没有放弃 正解就是下面这 完成重启即可 sudo cp /sys/fir ...