题解:P11369 [Ynoi2024] 弥留之国的爱丽丝

· · 题解

模拟赛考到这题,codeforces 题面没看到 directed,遂成小丑。不过这题在 Ynoi 里还算比较简单的。

分析

考虑时间轴重构。对一段时间未修改的黑边跑可达性,但时空都炸了,所以只考虑这段时间修改的边的端点与询问的点(称之为关键点),关键点只有 2 \cdot B 个,Tarjan + Topo 时复 \Theta( { q \over B } \cdot ( (n + m) + (n + m) \cdot { B \over \omega } ) ) 。对于询问,暴力加边压进 bitset 跑 BFS,时复 \mathcal{O}(q \cdot B \cdot {B \over \omega}) 。\ 总时复 \mathcal{O}( { q \cdot (n + m) \over \omega } + q \cdot ( { (n + m) \over B } + { B ^ 2 \over \omega } ) ) ,取 B = \omega ^ { 1 \over 3 } \cdot (n + m) ^ {1 \over 3} 最优,但重构常数大,所以 B 取 200 多一点最优,代入得时复 \mathcal{O}({q \cdot (n + m) + q ^ {2 \over 3 } \cdot (n + m) ^ {2 \over 3} \over \omega}) ,能过

AC code

#include <bits/stdc++.h>

namespace io
{
  char buf[1 << 20], *p1(buf), *p2(buf), c;
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)

  template <typename _Tp> inline void read(_Tp& x)
  { x = 0; while (!isdigit(c = gc())); while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = gc())); }

  template <typename _Tp, typename... Args> inline void read(_Tp& x, Args&... args) { read(x), read(args...); }

  char pbuf[1 << 20], *pp(pbuf), sta[30], *top(sta);
#define flush() (fwrite(pbuf, 1, pp - pbuf, stdout), pp = pbuf)
#define pc(c) (pp - pbuf == 1 << 20 && flush(), *pp++ = (c))

  template <typename _Tp> inline void write(_Tp x, const char& e = '\n')
  { while (*top++ = x % 10 ^ 48, x /= 10); while (pc(*--top), top != sta); pc(e); }

  struct flusher { inline ~flusher(void) { flush(); } } ioflush;  
} using io::read; using io::write;

using u32 = unsigned;
struct edge
{
  u32 u, v; inline edge(void) = default; inline edge(const edge& e) = default;
  inline edge(const u32& x, const u32& y) : u(x), v(y) { }
};
struct opt { u32 op, x, y; };

inline void chkmin(u32& a, const u32& b) { (a > b) && (a = b); }

constexpr u32 N(5e4+1), M(1e5+1), B(300), B2(700);

u32 n, m, q, b; edge E[M]; std::bitset<M> c, c0, f; opt op[M];
std::vector<u32> G[N], DAG[N]; u32 bel[N], tot, deg[N];
u32 ts, dfn[N], low[N], sta[N], top; std::bitset<N> in;
std::bitset<N> key, vis; u32 kp[B2], idx[N], cnt; std::bitset<B2> lnk[N], S[B2], cur;

void Tarjan(const u32& u)
{
  dfn[u] = low[u] = ++ts; sta[top++] = u; in.set(u);
  for (const u32& v : G[u]) if (!dfn[v]) Tarjan(v), chkmin(low[u], low[v]); else if (in[v]) chkmin(low[u], dfn[v]);
  if (dfn[u] == low[u]) { tot++; do { top--, bel[sta[top]] = tot, in.reset(sta[top]); } while (sta[top] != u); }
}

void DFS(const u32& u) { vis.set(u); for (const u32& v : DAG[u]) !vis[v] && (DFS(v), 0), lnk[u] |= lnk[v]; }

inline void Rebuild(void)
{
  for (u32 i(1); i <= n; i++) G[i].clear(), DAG[i].clear(), dfn[i] = 0, lnk[i].reset(); ts = tot = 0;
  for (u32 i(1); i <= m; i++) f[i] && (G[E[i].u].emplace_back(E[i].v), 0);
  for (u32 i(1); i <= n; i++) !dfn[i] && (Tarjan(i), 0);
  key.reset(); for (u32 i(1); i <= cnt; i++) key.set(bel[kp[i]]);
  cnt = 0; for (u32 i(1); i <= tot; i++) key[i] && (kp[idx[i] = ++cnt] = i);
  for (u32 i(1); i <= cnt; i++) lnk[kp[i]].set(i);
  for (u32 i(1); i <= n; i++) for (const u32& v : G[i]) (bel[i] != bel[v]) && (DAG[bel[i]].emplace_back(bel[v]), 0);
  vis.reset(); for (u32 i(1); i <= tot; i++) !vis[i] && (DFS(i), 0);
}

inline bool Judge(const u32& s, const u32& t, const u32& L, const u32& R)
{
  if (lnk[s][idx[t]]) return true;
  for (u32 i(1); i <= cnt; i++) S[i].reset();
  for (u32 i(L); i < R; i++) (op[i].op == 1) && c0[op[i].x] && (S[idx[bel[E[op[i].x].u]]].set(idx[bel[E[op[i].x].v]]), 0);
  static u32 que[N], head, tail, u; que[head = tail = 0] = s; vis.reset();
  while (head <= tail)
  {
    vis.set(u = que[head++]); if (lnk[u][idx[t]]) return true; cur = lnk[u] | S[idx[u]];
    for (u32 i(cur._Find_first()); i < cur.size(); i = cur._Find_next(i)) !vis[kp[i]] && (que[++tail] = kp[i]);
  } return false;
}

signed main(void)
{
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);
#endif

  u32 i, L, R, htop;

  read(n, m, q), b = sqrt(q); for (i = 1; i <= m; i++) read(E[i].u, E[i].v), c.set(i);

  for (L = 0; L < q; L = R)
  {
    R = L + b, (R > q) && (R = q); c0 = c;
    for (i = L; i < R; i++) read(op[i].op, op[i].x), (op[i].op == 1) ? c.flip(op[i].x) : (read(op[i].y), 0); key.reset(); f = c;
    for (i = L; i < R; i++) (op[i].op == 1) ? (key.set(E[op[i].x].u), key.set(E[op[i].x].v)) : (key.set(op[i].x), key.set(op[i].y));
    for (i = 1, cnt = 0; i <= n; i++) key[i] && (kp[++cnt] = i); for (i = L; i < R; i++) (op[i].op == 1) && (f.reset(op[i].x), 0); Rebuild();
    for (i = L; i < R; i++) { if (op[i].op == 1) c0.flip(op[i].x); else puts(Judge(bel[op[i].x], bel[op[i].y], L, R) ? "YES" : "NO"); }
  }

  return 0;
}