• 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
    ");
                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
    ", ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    IL汇编语言介绍(译)
    开源搜索框架Lucene学习系列
    【转】autoHeight为true的时候,autoScroll为true就不起作用了
    [转]反注册 Regsvr32命令应用大全
    [转]sun.misc.BASE64Encoder找不到的解决方法
    mysql数据库导入导出
    【转】MySQL 与MS SQL Server数据库使用多表关联Update时语法的区别
    CRT detected that the application wrote to memory after end of heap buffer
    魔兽争霸窗口化
    GROUP_CONCAT函数
  • 原文地址:https://www.cnblogs.com/cj-chd/p/10352143.html
Copyright © 2020-2023  润新知