Submission #500175

#TimeUsernameProblemLanguageResultExecution timeMemory
500175kamalsharmaa9Bitaro’s Party (JOI18_bitaro)C++14
0 / 100
4 ms7244 KiB
#include<bits/stdc++.h>
using namespace std;
#define N               100010
#define ff              first
#define ss              second
#define pb              push_back
#define pii             pair<int,int>
int B ;
int not_taken[N];
vector<int>gr[N];
vector<int>grr[N];
int dp[N];
bitset<N>rep;
vector<pii>dist[N];
vector<pii>child_and_dist;
bool compare(const pii &p1, const pii & p2)
{
  return p1.first > p2.first;
}
int32_t main()
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);


  int n, m, q;
  cin >> n >> m >> q;
  B = sqrt(n);
  B += 100;
  int a, b;
  for (int i = 1; i <= m; i++)
  {
    cin >> a >> b;
    gr[a].pb(b);
    grr[b].pb(a);
  }
///// precomputation
  for (int node = 1; node <= n; node++)
  {
    child_and_dist.clear();
    for (auto child : grr[node])
    {
      for (auto j : dist[child])
      {
        child_and_dist.pb({j.ff + 1, j.ss});
      }
    }
    child_and_dist.pb({0, node});
    int num_taken = 0;
    sort(child_and_dist.begin(), child_and_dist.end(), compare);
    for (int j = 0; j < (int)child_and_dist.size() && num_taken < B; j++)
    {
      if (!rep[child_and_dist[j].ss])
      {
        num_taken++;
        dist[node].pb({child_and_dist[j]});
        rep[child_and_dist[j].ss] = 1;
      }
    }
    for (int j = 0; j < (int)dist[node].size(); j++)
      rep[dist[node][j].ss] = 0;
  }
  for (int i = 0; i <= n; i++)
    not_taken[i] = 0;
  while (q--)
  {
    int p, q2;
    cin >> p >> q2;
    for (int i = 1; i <= q2; i++)
    {
      int r; cin >> r;
      not_taken[r] = q;
    }
    ///////////////////////////
    if (q < B)
    {
      int ans = -1;
      for (auto i : dist[p])
      {
        if (not_taken[i.second] == q )
          continue;
        ans = i.first;
        break;
      }
      cout << ans << '\n';
    }
    else
    {
      for (int i = 1; i <= p; i++)
      {
        dp[i] = 0;
      }
      for (int i = 1; i <= p; i++)
      {
        if (not_taken[i] == q && dp[i] == 0)
          continue;
        for (auto child : gr[i])
        {
          dp[child] = max(dp[child], dp[i] + 1);
        }
      }
      if (not_taken[p] == q && dp[p] == 0)
        dp[p] = -1;
      cout << dp[p] << '\n';
    }


  }

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...