题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1924
容易想到,如果在第
i
个藏宝宫室可以传送到第
但是,在建图上存在一个问题,从横天门或纵寰门引出的边可能特别多。在极端情况下,
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;
}