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;
#define N 100010
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
const int B = 200;
bool not_taken[N];
vector<int>gr[N];
vector<int>grr[N];
int dp[N];
bool rep[N];
vector<pii>dist[N];
int32_t main()
{
c_p_c();
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= m; i++)
{
int a, b;
cin >> a >> b;
gr[a].pb(b);
grr[b].pb(a);
}
///// precomputation
vector<pii>child_and_dist;
for (int node = 1; node <= n; node++)
{
for (auto child : grr[node])
{
for (auto j : dist[child])
{
child_and_dist.pb({j.ff + 1, j.ss});
}
}
child_and_dist.pb({0, node});
int num_taken = 0;
sort(child_and_dist.begin(), child_and_dist.end(), greater<pii>());
for (int j = 0; j < (int)child_and_dist.size() && num_taken < B; j++)
{
if (!rep[child_and_dist[j].ss])
{
num_taken++;
dist[node].pb({child_and_dist[j]});
rep[child_and_dist[j].ss] = 1;
}
}
for (int j = 0; j < dist[node].size(); j++)
rep[dist[node][j].ss] = 0;
}
for (int i = 0; i <= n; i++)
not_taken[i] = 0;
vector<int>not_take;
while (q--)
{
int p, q;
cin >> p >> q;
not_take.clear();
for (int i = 1; i <= q; i++)
{
int r; cin >> r;
not_take.pb(r);
not_taken[r] = 1;
}
///////////////////////////
if (q < B)
{
int ans = -1;
for (auto i : dist[p])
{
if (not_taken[i.second] )
continue;
ans = i.first;
break;
}
cout << ans << '\n';
}
else
{
for (int i = 1; i <= p; i++)
{
dp[i] = 0;
}
for (int i = 1; i <= p; i++)
{
if (not_taken[i] && dp[i] == 0)
continue;
for (auto child : gr[i])
{
dp[child] = max(dp[child], dp[i] + 1);
}
}
if (not_taken[p] && dp[p] == 0)
dp[p] = -1;
cout << dp[p] << '\n';
}
for (auto i : not_take)
not_taken[i] = 0;
}
}
Compilation message (stderr)
bitaro.cpp: In function 'int32_t main()':
bitaro.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j = 0; j < dist[node].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |