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 = 300;
int n, m, q;
vector<int> g[maxn];
bool bad[maxn];
int dp[maxn];
bool visited[maxn];
int c[maxn];
int calc(int node)
{
memset(dp, -1, sizeof(dp));
dp[node] = 0;
for (int i = node - 1; i >= 1; --i)
{
for (auto prv : g[i])
{
if (dp[prv] != -1)
{
dp[i] = max(dp[i], 1 + dp[prv]);
}
}
}
int ans = -1;
for (int i = node; i >= 1; --i)
{
if (bad[i] == false)
{
ans = max(ans, dp[i]);
}
}
for (int i = 1; i <= n; ++i)
{
bad[i] = false;
}
return ans;
}
pair<int, int> max_dist[maxn][SQRT + 5];
void precompute_small()
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j < SQRT; ++j)
{
max_dist[i][j].first = -1;
}
max_dist[i][0].first = 0;
max_dist[i][0].second = i;
}
for (int i = 1; i <= n; ++i)
{
for (auto to : g[i])
{
int ptr1 = 0;
int ptr2 = 0;
vector<pair<int, int>> combined;
int iter = 0;
while (true)
{
if (combined.size() >= SQRT)
{
break;
}
pair<int, int> curr = make_pair(0, 0);
if (max_dist[i][ptr1].first != -1 && max_dist[i][ptr1].first + 1 > max_dist[to][ptr2].first)
{
curr = make_pair(max_dist[i][ptr1].first + 1, max_dist[i][ptr1].second);
++ptr1;
}
else if (max_dist[to][ptr2].first != -1)
{
curr = make_pair(max_dist[to][ptr2].first, max_dist[to][ptr2].second);
++ptr2;
}
else
{
break;
}
if (visited[curr.second] == false)
{
visited[curr.second] = true;
combined.push_back(curr);
}
}
for (int j = 0; j < combined.size(); ++j)
{
max_dist[to][j] = combined[j];
}
for (int j = 0; j < combined.size(); ++j)
{
visited[combined[j].second] = false;
}
}
}
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> m >> q;
for (int i = 1; i <= m; ++i)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
}
precompute_small();
for (int i = 1; i <= q; ++i)
{
int node, cnt;
cin >> node >> cnt;
for (int j = 1; j <= cnt; ++j)
{
cin >> c[j];
bad[c[j]] = true;
}
if (cnt >= SQRT)
{
int ans = calc(node);
cout << ans << "\n";
}
else
{
bool flag = true;
for (int j = 0; j <= SQRT; ++j)
{
if (max_dist[node][j].first < 0)
{
flag = false;
break;
}
if (bad[max_dist[node][j].second] == false)
{
cout << max_dist[node][j].first << "\n";
break;
}
}
if (flag == false)
{
cout << -1 << "\n";
}
for (int j = 1; j <= cnt; ++j)
{
bad[c[j]] = false;
}
}
}
return 0;
}
Compilation message (stderr)
bitaro.cpp: In function 'void precompute_small()':
bitaro.cpp:110:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int j = 0; j < combined.size(); ++j)
| ~~^~~~~~~~~~~~~~~~~
bitaro.cpp:115:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for (int j = 0; j < combined.size(); ++j)
| ~~^~~~~~~~~~~~~~~~~
bitaro.cpp:77:17: warning: unused variable 'iter' [-Wunused-variable]
77 | int iter = 0;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |