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>
#define ll long long
#define pii pair<int, int>
#define endl '\n'
using namespace std;
const int BLOCK_SZ = 300;
const int MAX_N = 1e5 + 1;
vector<int> graph[MAX_N];
bool mark[MAX_N];
vector<pii> paths[MAX_N];
int dfs(int v)
{
int ret = mark[v] ? -1 : 0;
for (int u : graph[v])
{
int res = dfs(u);
if (res != -1)
ret = max(ret, res + 1);
}
return ret;
}
void pre_comp(int v)
{
vector<pii> cur;
for (int u : graph[v])
for (pii pth : paths[u])
cur.push_back({pth.first + 1, pth.second});
cur.push_back({0, v});
sort(cur.begin(), cur.end(), greater<pii>());
for (pii &p : cur)
{
if (!mark[p.second])
{
paths[v].push_back(p);
mark[p.second] = true;
}
}
for (pii &p : cur)
mark[p.second] = false;
while (paths[v].size() > BLOCK_SZ)
paths[v].pop_back();
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, M, Q;
cin >> N >> M >> Q;
for (int i = 0; i < M; i++)
{
int a, b;
cin >> a >> b;
--a;
--b;
graph[b].push_back(a);
}
for (int v = 0; v < N; v++)
pre_comp(v);
while (Q--)
{
int t, y;
cin >> t >> y;
--t;
vector<int> cs(y);
for (int i = 0; i < y; i++)
{
cin >> cs[i];
--cs[i];
mark[cs[i]] = true;
}
if (y >= BLOCK_SZ)
cout << dfs(t) << endl;
else
{
bool ok = false;
for (pii pth : paths[t])
{
if (!mark[pth.second])
{
cout << pth.first << endl;
ok = true;
break;
}
}
if (!ok)
cout << -1 << endl;
}
for (int c : cs)
mark[c] = 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... |