[BZOJ1924][SDOI2010]所驼门王的宝藏(Tarjan+拓扑排序)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1924
容易想到,如果在第 i 个藏宝宫室可以传送到第j个藏宝宫室,就连一条 i>j 的有向边, Tarjan 强连通分量缩点之后,在新图上找出一条路径,使得这条路径上的所有点对应的强连通分量大小之和最大,这个可以按照拓扑序进行递推来实现。
但是,在建图上存在一个问题,从横天门或纵寰门引出的边可能特别多。在极端情况下, 105 个横天门在同一行里出现,这样时空复杂度都是无法承受的。考虑这一点进行优化:由于在同一行的横天门一定属于同一个强连通分量,所以在建边时,只需要对在同一行的横天门构建出一个环即可,不需要两两进行连边。而此行内的其他宫室,只需要从这个环中的任意一点向这个宫室连边即可。对于纵寰门也是一样。
此外,在建图的实现上有一些小技巧,具体见代码。
代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
    int res = 0; bool bo = 0; char c;
    while (((c = getchar()) < '0' || c > '9') && c != '-');
    if (c == '-') bo = 1; else res = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        res = (res << 3) + (res << 1) + (c - 48);
    return bo ? ~res + 1 : res;
}
const int N = 1e5 + 5, M = 2e6 + 5, R = 1e6 + 5;
int n, ecnt, nxt[M], adj[N], go[M], dfn[N], low[N], times, sum, num[N],
top, stk[N], bel[N], ecnt2, nxt2[M], adj2[N], go2[M], tot, st[N], ed[N],
las[R], now[R], nex[R], row[N], cnt[N], res[N], H, T, Q[N]; bool ins[N];
struct cyx {int x, y, t, id;} a[N];
void add_edge(int u, int v) {
    nxt[++ecnt] = adj[u]; adj[u] = ecnt; go[ecnt] = v;
}
void add_edge2(int u, int v) {
    nxt2[++ecnt2] = adj2[u]; adj2[u] = ecnt2; go2[ecnt2] = v;
}
bool comp1(cyx a, cyx b) {
    if (a.x != b.x) return a.x < b.x;
    return a.t < b.t;
}
bool comp2(cyx a, cyx b) {
    if (a.y != b.y) return a.y < b.y;
    return a.t < b.t;
}
bool comp3(cyx a, cyx b) {
    if (a.x != b.x) return a.x < b.x;
    return a.y < b.y;
}
void Tarjan(int u) {
    dfn[u] = low[u] = ++times; ins[stk[++top] = u] = 1;
    for (int e = adj[u], v; e; e = nxt[e])
        if (!dfn[v = go[e]]) {
            Tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (ins[v]) low[u] = min(low[u], dfn[v]);
    if (dfn[u] == low[u]) {
        num[bel[u] = ++sum] = 1; ins[u] = 0; int v;
        while (v = stk[top--], v != u) num[bel[v] = sum]++, ins[v] = 0;
    }
}
void topo() {
    int i; H = T = 0;
    for (i = 1; i <= sum; i++) if (!cnt[i]) Q[++T] = i, res[i] = num[i];
    while (H < T) {
        int u = Q[++H];
        for (int e = adj2[u], v; e; e = nxt2[e]) {
            if (!(--cnt[v = go2[e]])) Q[++T] = v;
            res[v] = max(res[v], res[u] + num[v]);
        }
    }
}
int main() {
    int i, j; n = read(); read(); read();
    for (i = 1; i <= n; i++) a[i].x = read(), a[i].y = read(),
        a[i].t = read(), a[i].id = i;
    sort(a + 1, a + n + 1, comp1);
    for (i = 1; i <= n;) {
        int fir = 0, lst = 0;
        for (j = i; j <= n && a[i].x == a[j].x; j++) if (a[j].t == 1) {
            if (!fir) fir = j; lst = j;
            if (j < n && a[i].x == a[j + 1].x && a[j + 1].t == 1)
                add_edge(a[j].id, a[j + 1].id);
        }
        if (lst) {
            if (lst != fir) add_edge(a[lst].id, a[fir].id);
            for (j = i; j <= n && a[i].x == a[j].x; j++) if (a[j].t != 1)
                add_edge(a[lst].id, a[j].id);
        }
        i = j;
    }
    sort(a + 1, a + n + 1, comp2);
    for (i = 1; i <= n;) {
        int fir = 0, lst = 0;
        for (j = i; j <= n && a[i].y == a[j].y; j++) if (a[j].t == 2) {
            if (!fir) fir = j; lst = j;
            if (j < n && a[i].y == a[j + 1].y && a[j + 1].t == 2)
                add_edge(a[j].id, a[j + 1].id);
        }
        if (lst) {
            if (lst != fir) add_edge(a[lst].id, a[fir].id);
            for (j = i; j <= n && a[i].y == a[j].y; j++) if (a[j].t != 2)
                add_edge(a[lst].id, a[j].id);
        }
        i = j;
    }
    sort(a + 1, a + n + 1, comp3);
    for (i = 1; i <= n;) {
        st[++tot] = i; row[tot] = a[i].x;
        for (j = i; j <= n && a[i].x == a[j].x; j++);
        ed[tot] = j - 1; i = j;
    }
    for (i = 1; i <= tot; i++) {
        if (i > 1) for (j = st[i - 1]; j <= ed[i - 1]; j++)
            las[a[j].y] = a[j].id;
        for (j = st[i]; j <= ed[i]; j++) now[a[j].y] = a[j].id;
        if (i < tot) for (j = st[i + 1]; j <= ed[i + 1]; j++)
            nex[a[j].y] = a[j].id;
        for (j = st[i]; j <= ed[i]; j++) {
            if (a[j].t != 3) continue;
            if (i > 1 && row[i - 1] + 1 == row[i]) {
                if (las[a[j].y - 1]) add_edge(a[j].id, las[a[j].y - 1]);
                if (las[a[j].y]) add_edge(a[j].id, las[a[j].y]);
                if (las[a[j].y + 1]) add_edge(a[j].id, las[a[j].y + 1]);
            }
            if (now[a[j].y - 1]) add_edge(a[j].id, now[a[j].y - 1]);
            if (now[a[j].y + 1]) add_edge(a[j].id, now[a[j].y + 1]);
            if (i < tot && row[i + 1] - 1 == row[i]) {
                if (nex[a[j].y - 1]) add_edge(a[j].id, nex[a[j].y - 1]);
                if (nex[a[j].y]) add_edge(a[j].id, nex[a[j].y]);
                if (nex[a[j].y + 1]) add_edge(a[j].id, nex[a[j].y + 1]);
            }
        }
        if (i > 1) for (j = st[i - 1]; j <= ed[i - 1]; j++)
            las[a[j].y] = 0;
        for (j = st[i]; j <= ed[i]; j++) now[a[j].y] = 0;
        if (i < tot) for (j = st[i + 1]; j <= ed[i + 1]; j++)
            nex[a[j].y] = 0;
    }
    for (i = 1; i <= n; i++) if (!dfn[i]) Tarjan(i);
    for (i = 1; i <= n; i++) for (int e = adj[i]; e; e = nxt[e])
        if (bel[i] != bel[go[e]]) add_edge2(bel[i], bel[go[e]]),
            cnt[bel[go[e]]]++;
    int ans = 0; topo();
    for (i = 1; i <= sum; i++) ans = max(ans, res[i]);
    printf("%d\n", ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值