BZOJ2815 [ZJOI2012]灾难

58 篇文章 0 订阅

Address

Solution

  • 考虑建出这样的一棵树,当树上的某一个点 x 灭绝时,它子树中的点也都会灭绝。
  • 则点 x 的灾难值就转变为在这棵树中以 x 为根的子树大小减一。
  • 那么,如何建出这棵树呢?
  • 对于每次加入的点 x,我们把它挂在它所有食物的LCA下面。
  • 这样,一旦该LCA灭绝,点 x 的所有食物灭绝了,得不到食物的点 x 也就灭绝了,这时 x 为该LCA的子节点,符合我们之前对这棵树的定义。
  • 按照这样建树,点 x 的所有食物要在点 x 之前加入这棵树,而原图是有向无环图,所以这个顺序显然就是拓扑序,而每加入一个点后,LCA也可以用倍增动态维护。
  • 要注意如果有多个生产者,原图建完后可能是一个森林,为了方便处理我们虚拟一个点为所有生产者的食物,也就是树的根。
  • 时间复杂度 O(nlogn+m)

Code

#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>

using namespace std;

namespace INOUT
{
    const int S = 1 << 20;
    char frd[S], *hed = frd + S;
    const char *tal = hed;

    inline char nxtChar()
    {
        if (hed == tal)
            fread(frd, 1, S, stdin), hed = frd;
        return *hed++;
    }

    inline int get()
    {
        char ch; int res = 0; bool flag = false;
        while (!isdigit(ch = nxtChar()) && ch != '-');
        (ch == '-' ? flag = true : res = ch ^ 48);
        while (isdigit(ch = nxtChar()))
            res = res * 10 + ch - 48;
        return flag ? -res : res;
    }

    inline void put(int x)
    {
        if (x > 9) put(x / 10);
        putchar(x % 10 + 48);
    }
};

using namespace INOUT;

const int N = 7e4 + 5, M = N << 2;
int dep[N], f[N][18], sze[N];
int rin[N], pos[N], stk[N];
int n, E, top;

struct Edge
{
    int to; Edge *nxt;
};

Edge p[M], *T = p, *lst[N];
Edge q[M], *Q = q, *rst[N];
Edge w[M], *W = w, *wst[N];

inline void LinkEdge(int x, int y)
{
    (++T)->nxt = lst[x]; lst[x] = T; T->to = y; ++rin[y];
    (++W)->nxt = wst[y]; wst[y] = W; W->to = x; 
}

inline void RinkEdge(int x, int y)
{
    (++Q)->nxt = rst[x]; rst[x] = Q; Q->to = y;
}

inline int LCAquery(int x, int y)
{
    if (x == y) return x;
    if (dep[x] < dep[y]) swap(x, y);

    for (int i = 16; i >= 0; --i)
    {
        if (dep[f[x][i]] >= dep[y]) x = f[x][i];
        if (x == y) return x;
    }

    for (int i = 16; i >= 0; --i)
        if (f[x][i] != f[y][i])
            x = f[x][i], y = f[y][i];
    return f[x][0];
}

inline void Dfs(int x)
{
    sze[x] = 1;
    for (Edge *e = rst[x]; e; e = e->nxt)
        Dfs(e->to), sze[x] += sze[e->to];
}

int main()
{
    freopen("catas.in", "r", stdin);
    freopen("catas.out", "w", stdout);

    n = get(); int x, y;
    for (int i = 1; i <= n; ++i)
    {
        x = get();
        while (x) 
            LinkEdge(x, i), x = get();
    }

    for (int i = 1; i <= n; ++i)
        if (!rin[i]) stk[++top] = pos[++E] = i;
    while (top)
    {
        x = stk[top--];
        for (Edge *e = lst[x]; e; e = e->nxt)
            if (!--rin[y = e->to]) stk[++top] = pos[++E] = y;
    }

    for (int i = 1; x = pos[i], i <= E; ++i)
    {
        int lca = -1;
        for (Edge *e = wst[x]; e; e = e->nxt)
            if (lca == -1)
                lca = e->to;
            else 
                lca = LCAquery(lca, e->to);
        if (lca == -1) lca = 0;

        RinkEdge(lca, x); 
        f[x][0] = lca; dep[x] = dep[lca] + 1;
        for (int j = 0; j < 16; ++j)
            f[x][j + 1] = f[f[x][j]][j];
    }

    Dfs(0);
    for (int i = 1; i <= n; ++i)
        put(sze[i] - 1), putchar('\n');
    fclose(stdin); fclose(stdout);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值