This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Smaug never desolated!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = (int)100007;
const int block = 320;
const int infint = (int)1e8 + 3;
const int MOD = (int)1e9 + 7;
const ll inf = (ll)1e18;
int n, m, q, bad[MAXN], dp[MAXN], input[MAXN];
pair<int, int> DP[MAXN][block], tmp[block];
vector<int> in[MAXN];
bool cmp(pair<int, int> a, pair<int, int> b)
{
return a.second > b.second;
}
void merge(int a, int b)
{
for (int i = 0; i < block; i++)
tmp[i] = {-1, -1};
int ptr1 = 0, ptr2 = 0;
while(ptr1 + ptr2 < block)
{
pair<int, int> B = DP[b][ptr2];
if(B.second != -1)
B.second++;
if(cmp(DP[a][ptr1], B))
tmp[ptr1 + ptr2] = DP[a][ptr1], ptr1++;
else
tmp[ptr1 + ptr2] = B, ptr2++;
}
for (int i = 0; i < block; i++)
DP[a][i] = tmp[i];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
in[v].push_back(u);
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < block; j++)
DP[i][j] = {-1, -1};
DP[1][0] = {1, 0};
for (int i = 2; i <= n; i++)
{
DP[i][0] = {i, 0};
for (auto u : in[i])
merge(i, u);
}
for (int i = 0; i < q; i++)
{
int v, t;
cin >> v >> t;
for (int i = 0; i < t; i++)
{
cin >> input[i];
bad[input[i]] = 1;
}
//answer query.
if(t >= block)
{
memset(dp, -1, sizeof dp);
dp[v] = 0;
for (int i = v; i >= 1; i--)
if(dp[i] != -1)
for (auto u : in[i])
dp[u] = max(dp[u], dp[i] + 1);
int ans = -1;
for (int i = v; i >= 1; i--)
if(!bad[i])
ans = max(ans, dp[i]);
cout << ans << "\n";
}
else
{
int ans = -1;
for (int i = block - 1; i >= 0; i--)
if(DP[v][i].first != -1 && !bad[DP[v][i].first])
ans = DP[v][i].second;
cout << ans << "\n";
}
//
for (int i = 0; i < t; i++)
bad[input[i]] = 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... |