제출 #231554

#제출 시각아이디문제언어결과실행 시간메모리
231554xiaowuc1Bitaro’s Party (JOI18_bitaro)C++14
7 / 100
2086 ms16592 KiB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x).size()
#define derr if(1) cerr
typedef vector<int> vi;
// END NO SAD

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int SAVE = 300;
vector<pii> best[100005];
priority_queue<pii> qbest[100005];
vector<int> edges[100005];

int excluded[100005];
int n;
int dp[100005];
void solve(int dest, int id) {
  memset(dp, -1, sizeof(dp));
  for(int i = 1; i <= dest; i++) {
    if(excluded[i] != id) dp[i] = max(dp[i], 0);
    if(dp[i] == -1) continue;
    for(int out: edges[i]) {
      dp[out] = max(dp[out], dp[i] + 1);
    }
  }
  cout << dp[dest] << "\n";
}

void solve() {
  int q;
  {
    int m;
    cin >> n >> m >> q;
    while(m--) {
      int a, b;
      cin >> a >> b;
      edges[a].push_back(b);
    }
  }
  for(int i = 1; i <= n; i++) {
    qbest[i].push(pii(0, i));
  }
  for(int i = 1; i <= n; i++) {
    while(sz(qbest[i])) {
      int a, b;
      tie(a, b) = qbest[i].top(); qbest[i].pop();
      best[i].emplace_back(-a, b);
    }
    sort(rall(best[i]));
    for(int out: edges[i]) {
      for(pii val: best[i]) {
        qbest[out].push(pii(-(val.first + 1), val.second));
        if(sz(qbest[out]) > SAVE) qbest[out].pop();
      }
    }
  }
  for(int qq = 1; qq <= q; qq++) {
    int dest;
    cin >> dest;
    int k;
    cin >> k;
    while(k--) {
      int x;
      cin >> x;
      excluded[x] = qq;
    }
    int ret = -2;
    bool found = false;
    for(pii out: best[dest]) {
      if(excluded[out.second] == qq) continue;
      ret = out.first;
      found = true;
      break;
    }
    if(found) {
      cout << ret << "\n";
      continue;
    }
    solve(dest, qq);
  }
}


// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...