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;
int BLOCK_SZ = 100;
vector<vector<int>> graph;
vector<bool> mark;
vector<vector<pii>> paths;
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;
graph = vector<vector<int>>(N);
mark = vector<bool>(N, false);
paths = vector<vector<pii>>(N);
// BLOCK_SZ = sqrt(N);
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);
if (N >= 1000)
assert(1 != 1);
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;
}
}
Compilation message (stderr)
bitaro.cpp: In function 'void pre_comp(int)':
bitaro.cpp:48:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
48 | while (paths[v].size() > BLOCK_SZ)
| ~~~~~~~~~~~~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |