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;
vector<vector<unsigned>> g;
vector<vector<pair<unsigned, unsigned>>> dp;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, m, q;
cin >> n >> m >> q;
g = vector<vector<unsigned>>(n);
dp = vector<vector<pair<unsigned, unsigned>>>(n);
for (size_t i = 0; i < n; i++)
dp[i].emplace_back(0, i);
for (size_t i = 0; i < m; i++)
{
unsigned u, v;
cin >> u >> v;
g[u - 1].push_back(v - 1);
}
size_t const s = 316;
vector<bool> used(n, 0);
for (size_t i = 0; i < n; i++)
{
for (unsigned const &j : g[i])
{
size_t u = 0, v = 0;
vector<pair<unsigned, unsigned>> new_dp;
while (u < dp[i].size() && v < dp[j].size())
{
if (dp[i][u].first + 1 > dp[j][v].first)
{
new_dp.emplace_back(dp[i][u].first + 1, dp[i][u].second);
u++;
}
else
{
new_dp.push_back(dp[j][v]);
v++;
}
}
while (u < dp[i].size())
new_dp.emplace_back(dp[i][u].first + 1, dp[i][u].second), u++;
while (v < dp[j].size())
new_dp.push_back(dp[j][v]), v++;
dp[j].clear();
for (size_t k = 0; k < new_dp.size() && dp[j].size() < s; k++)
if (!used[new_dp[k].second])
{
dp[j].push_back(new_dp[k]);
used[new_dp[k].second] = 1;
}
for (size_t k = 0; k < new_dp.size(); k++)
used[new_dp[k].second] = 0;
}
}
vector<bool> blocked(n, 0);
while (q--)
{
unsigned t, y;
cin >> t >> y;
t--;
vector<unsigned> z;
for (size_t i = 0; i < y; i++)
{
unsigned u;
cin >> u;
blocked[u - 1] = 1;
z.push_back(u - 1);
}
if (y < s)
{
bool found = 0;
for (size_t i = 0; i < dp[t].size(); i++)
if (!blocked[dp[t][i].second])
{
found = 1;
cout << dp[t][i].first << '\n';
break;
}
if (!found)
cout << -1 << '\n';
}
else
{
vector<int> dp2(n, 0);
for (size_t i = 0; i < n; i++)
if (blocked[i])
dp2[i] = -1;
for (size_t i = 0; i < n; i++)
if (dp2[i] != -1)
for (unsigned const &j : g[i])
dp2[j] = max(dp2[j], dp2[i] + 1);
cout << dp2[t] << '\n';
}
for (unsigned const &u : z)
blocked[u] = 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... |