QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#343914#4000. Dynamic Reachabilityhos_lyricWA 1ms3664kbC++145.9kb2024-03-03 09:07:212024-03-03 09:07:21

Judging History

你现在查看的是最新测评结果

  • [2024-03-03 09:07:21]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3664kb
  • [2024-03-03 09:07:21]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


struct Scc {
  int n;
  vector<int> as, bs;
  vector<int> ptr, zu, us;

  int l;
  vector<int> ids;
  int operator[](int u) const { return ids[u]; }

  explicit Scc(int n_) : n(n_), as(), bs(), l(-1) {}
  void ae(int u, int v) {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    as.push_back(u);
    bs.push_back(v);
  }

  void dfs0(int u) {
    if (!ids[u]) {
      ids[u] = -1;
      for (int i = ptr[u]; i < ptr[u + 1]; ++i) dfs0(zu[i]);
      us.push_back(u);
    }
  }
  void dfs1(int u) {
    if (!~ids[u]) {
      ids[u] = l;
      for (int i = ptr[u]; i < ptr[u + 1]; ++i) dfs1(zu[i]);
    }
  }
  void run() {
    const int m = as.size();
    ptr.resize(n + 2);
    zu.resize(m);
    for (int u = 0; u < n + 2; ++u) ptr[u] = 0;
    for (int i = 0; i < m; ++i) ++ptr[as[i] + 2];
    for (int u = 2; u < n + 1; ++u) ptr[u + 1] += ptr[u];
    for (int i = 0; i < m; ++i) zu[ptr[as[i] + 1]++] = bs[i];
    ids.assign(n, 0);
    us.clear();
    for (int u = 0; u < n; ++u) dfs0(u);
    for (int u = 0; u < n + 2; ++u) ptr[u] = 0;
    for (int i = 0; i < m; ++i) ++ptr[bs[i] + 2];
    for (int u = 2; u < n + 1; ++u) ptr[u + 1] += ptr[u];
    for (int i = 0; i < m; ++i) zu[ptr[bs[i] + 1]++] = as[i];
    l = 0;
    for (int j = n; --j >= 0; ) if (!~ids[us[j]]) { dfs1(us[j]); ++l; }
  }

  vector<vector<int>> group() const {
    assert(~l);
    vector<vector<int>> uss(l);
    for (int u = 0; u < n; ++u) uss[ids[u]].push_back(u);
    return uss;
  }
};


#ifdef LOCAL
constexpr int K = 4;
#else
constexpr int K = 128;
#endif
using BS = bitset<2 * K>;

int N, M, Q;
vector<int> A, B;
vector<int> O, I, U, V;

int main() {
  for (; ~scanf("%d%d%d", &N, &M, &Q); ) {
    A.resize(M);
    B.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    O.resize(Q);
    I.assign(Q, -1);
    U.assign(Q, -1);
    V.assign(Q, -1);
    for (int q = 0; q < Q; ++q) {
      scanf("%d", &O[q]);
      if (O[q] == 1) {
        scanf("%d", &I[q]);
        --I[q];
      } else if (O[q] == 2) {
        scanf("%d%d", &U[q], &V[q]);
        --U[q];
        --V[q];
      } else {
        assert(false);
      }
    }
    
    vector<int> on(M, 1);
    vector<int> su(N, -1);
    for (int qL = 0, qR; qL < Q; qL = qR) {
      qR = min(qL + K, Q);
      vector<int> us, is;
      for (int q = qL; q < qR; ++q) {
        if (O[q] == 1) {
          const int i = I[q];
          on[i] ^= 2;
          us.push_back(A[i]);
          us.push_back(B[i]);
          is.push_back(i);
        } else if (O[q] == 2) {
          us.push_back(U[q]);
          us.push_back(V[q]);
        } else {
          assert(false);
        }
      }
      sort(us.begin(), us.end());
      us.erase(unique(us.begin(), us.end()), us.end());
      const int usLen = us.size();
      for (int e = 0; e < usLen; ++e) su[us[e]] = e;
      sort(is.begin(), is.end());
      is.erase(unique(is.begin(), is.end()), is.end());
      
      Scc scc(N);
      for (int i = 0; i < M; ++i) if (on[i] == 1) {
        scc.ae(A[i], B[i]);
      }
      scc.run();
      vector<vector<int>> hparg(scc.l);
      for (int i = 0; i < M; ++i) if (on[i] == 1) {
        const int x = scc[A[i]];
        const int y = scc[B[i]];
        if (x != y) {
          hparg[y].push_back(x);
        }
      }
// cerr<<"us = "<<us<<", scc.group() = "<<scc.group()<<", hparg = "<<hparg<<endl;
      // reachable? to each u in us
      vector<BS> dp(scc.l);
      for (int e = 0; e < usLen; ++e) {
        dp[scc[us[e]]].set(e);
      }
      for (int y = scc.l; --y >= 0; ) {
        for (const int x : hparg[y]) {
          dp[x] |= dp[y];
        }
      }
// for(int x=0;x<scc.l;++x){cerr<<"dp["<<x<<"] = ";for(int e=0;e<usLen;++e)cerr<<dp[x][e];cerr<<endl;}
      
      BS adj[2 * K];
      for (int q = qL; q < qR; ++q) {
        if (O[q] == 1) {
          const int i = I[q];
          on[i] ^= 1;
        } else if (O[q] == 2) {
          // BFS on us
          for (int e = 0; e < usLen; ++e) adj[e] = dp[scc[us[e]]];
          for (const int i : is) if (on[i] & 1) adj[su[A[i]]].set(su[B[i]]);
          BS vis, seen;
          vis.set(su[U[q]]);
          for (; ; ) {
            const int e = (vis & ~seen)._Find_first();
            if (e >= usLen) break;
            seen.set(e);
            vis |= adj[e];
          }
          puts(vis[su[V[q]]] ? "YES" : "NO");
        } else {
          assert(false);
        }
      }
      
      for (int q = qL; q < qR; ++q) if (O[q] == 1) on[I[q]] ^= 2;
      for (int e = 0; e < usLen; ++e) su[us[e]] = -1;
    }
  }
  return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3664kb

input:

5 6 7
1 2
1 3
2 4
3 4
3 5
4 5
2 1 5
2 2 3
1 3
1 4
2 1 4
1 3
2 1 5

output:

YES
NO
YES
YES

result:

wrong answer 3rd lines differ - expected: 'NO', found: 'YES'