This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5, kx=330;
int n, m, Q, a, b, sz, l, ans, x, idx[nx];
pair<int, int> mx[nx][kx];
vector<int> d[nx], rv[nx], vs(nx), dp(nx);
queue<int> q;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m>>Q;
for (int i=1; i<=m; i++) cin>>a>>b, d[a].push_back(b), rv[b].push_back(a);
//for (int i=1; i<=n; i++) for (int j=0; j<kx; j++) mx[i][j]={-1, 0};
for (int i=1; i<=n; i++)
{
for (auto v:rv[i]) idx[v]=0;
for (int j=0; j<kx; j++)
{
pair<int, pair<int, int>> res={-1, {0, -1}};
for (auto v:rv[i])
{
while (idx[v]<kx&&mx[v][idx[v]].first>=0&&vs[mx[v][idx[v]].second]) idx[v]++;
if (idx[v]<kx&&mx[v][idx[v]].first>=0)
{
if (res.first<mx[v][idx[v]].first+1) res={mx[v][idx[v]].first+1, {mx[v][idx[v]].second, v}};
}
}
if (res.first>=0)
{
idx[res.second.second]++, vs[res.second.first]=1, q.push(res.second.first);
}
mx[i][j]={res.first, res.second.first};
}
for (int j=0; j<kx; j++)
{
if (mx[i][j].first<0) {
mx[i][j]={0, i};
break;
}
}
while (!q.empty()) vs[q.front()]=0, q.pop();
}
//for (int i=1; i<=n; i++) for (int j=0; j<5; j++) cout<<i<<' '<<j<<' '<<mx[i][j].first<<' '<<mx[i][j].second<<'\n';
while (Q--)
{
cin>>l>>sz;
for (int i=1; i<=sz; i++) cin>>x, q.push(x), vs[x]=1;
if (sz<kx-1)
{
bool pt=0;
for (int i=0; i<kx&&!pt; i++) if (mx[l][i].first>=0&&!vs[mx[l][i].second]) cout<<mx[l][i].first<<'\n', pt=1;
if (!pt) cout<<-1<<'\n';
}
else
{
for (int i=1; i<=n; i++) dp[i]=INT_MIN;
dp[l]=0;
ans=-1;
for (int i=l; i>=1; i--)
{
for (auto v:d[i]) dp[i]=max(dp[i], dp[v]+1);
if (!vs[i]) ans=max(ans, dp[i]);
}
for (int i=1; i<=n; i++) vs[i]=0;
cout<<ans<<'\n';
}
while (!q.empty()) vs[q.front()]=0, q.pop();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |