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 MAXN = 100005;
const int SQRT = 250;
int n, m, q;
int dp[MAXN];
bool Mark[MAXN];
vector <int> Adj[MAXN];
vector <pair <int, int> > Dist[MAXN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for(int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
Adj[v].push_back(u);
}
for(int i = 1; i <= n; i++)
{
set <int> S;
for(auto x : Adj[i])
{
for(auto y : Dist[x])
{
dp[y.second] = max(dp[y.second], y.first + 1);
S.insert(y.second);
}
}
S.insert(i);
vector <pair <int, int> > tmp;
for(auto x : S)
{
tmp.emplace_back(dp[x], x);
dp[x] = 0;
}
sort(tmp.begin(), tmp.end());
while(tmp.empty() == false and Dist[i].size() < SQRT)
{
Dist[i].push_back(tmp.back());
tmp.pop_back();
}
}
while(q--)
{
int t, sz;
cin >> t >> sz;
vector <int> tmp(sz);
for(auto &x : tmp)
{
cin >> x;
Mark[x] = true;
}
bool ok = false;
for(int i = 0; i < (int)Dist[t].size(); i++)
{
if(!Mark[Dist[t][i].second])
{
ok = true;
cout << Dist[t][i].first << '\n';
break;
}
}
if(!ok)
{
int ans = -1;
for(int i = 1; i <= n; i++)
{
dp[i] = -1e9;
}
dp[t] = 0;
for(int i = t; i >= 1; i--)
{
for(auto x : Adj[i])
{
dp[x] = max(dp[x], dp[i] + 1);
}
if(!Mark[i])
{
ans = max(ans, dp[i]);
}
}
cout << ans << '\n';
}
for(auto x : tmp)
{
Mark[x] = false;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |