제출 #44037

#제출 시각아이디문제언어결과실행 시간메모리
44037wxh010910Bitaro’s Party (JOI18_bitaro)C++17
100 / 100
1040 ms272436 KiB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 100005;
const int M = 325;

int n, m, q, tim, top, f[N], seq[N], siz[N], vis[N];
vector <int> adj[N], rev[N];
pii dis[N][M];
bool ban[N];

inline void Merge(int x, int y) {
  static pii tmp[M << 1];
  ++tim, top = 0;
  for (int i = 1, j = 1; i <= siz[x] || j <= siz[y]; ) {
    if (i <= siz[x] && (j > siz[y] || dis[x][i].X >= dis[y][j].X + 1)) {
      if (vis[dis[x][i].Y] != tim) {
        vis[dis[x][i].Y] = tim, tmp[++top] = dis[x][i];
      }
      ++i;
    } else {
      if (vis[dis[y][j].Y] != tim) {
        vis[dis[y][j].Y] = tim, tmp[++top] = mp(dis[y][j].X + 1, dis[y][j].Y);
      }
      ++j;
    }
  }
  CheckMin(top, 320);
  siz[x] = top;
  for (int i = 1; i <= siz[x]; ++i) {
    dis[x][i] = tmp[i];
  }
}

inline void Brute(int s) {
  int ans = -1;
  f[s] = 0;
  if (!ban[s]) {
    ans = 0;
  }
  for (int i = s - 1; i; --i) {
    f[i] = -n;
    for (auto j : adj[i]) {
      if (j <= s) {
        CheckMax(f[i], f[j] + 1);
      }
    }
    if (!ban[i]) {
      CheckMax(ans, f[i]);
    }
  }
  printf("%d\n", ans);
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(m), Read(q);
  for (int i = 1, x, y; i <= m; ++i) {
    Read(x), Read(y), adj[x].pb(y), rev[y].pb(x);
  }
  for (int x = 1; x <= n; ++x) {
    siz[x] = 1, dis[x][1] = mp(0, x);
    for (auto y : rev[x]) {
      Merge(x, y);
    }
  }
  for (int i = 1, x, k; i <= q; ++i) {
    Read(x), Read(k);
    for (int j = 1; j <= k; ++j) {
      Read(seq[j]), ban[seq[j]] = true;
    }
    if (k < 320) {
      bool flg = false;
      for (int j = 1; j <= siz[x]; ++j) {
        if (!ban[dis[x][j].Y]) {
          printf("%d\n", dis[x][j].X);
          flg = true;
          break;
        }
      }
      if (!flg) {
        puts("-1");
      }
    } else {
      Brute(x);
    }
    for (int j = 1; j <= k; ++j) {
      ban[seq[j]] = false;
    }
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...