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 all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int B = 321;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q; cin >> n >> m >> q;
vector <vector<int>> G(n);
for ( int i = 1; i <= m; i++ ){
int u, v; cin >> u >> v;
--u, --v;
G[u].pb(v);
}
vector <vector<int>> dp(n, vector <int> (B, -1)), fa(n, vector <int> (B, -1));
for ( int i = 0; i < n; i++ ){
dp[i][0] = 0;
fa[i][0] = i;
}
vector <int> aux(n);
for ( int u = 0; u < n; u++ ){
for ( auto &v: G[u] ){
vector <int> tmp(B, -1), tmp_(B, -1), er;
int L = 0, R = 0, sz = 0;
while ( L < B || R < B ){
if ( sz == B ) break;
{
while ( L < B && (fa[u][L] != -1 && aux[fa[u][L]]) ) L++;
while ( R < B && (fa[v][R] != -1 && aux[fa[v][R]]) ) R++;
if ( L < B && fa[u][L] == -1 ) L = B;
if ( R < B && fa[v][R] == -1 ) R = B;
}
if ( L == B && R == B ) break;
if ( R == B || (L < B && dp[u][L] + 1 > dp[v][R]) ){
aux[fa[u][L]] = true;
er.pb(fa[u][L]);
tmp_[sz] = fa[u][L];
tmp[sz++] = dp[u][L] + 1;
L++;
} else{
aux[fa[v][R]] = true;
er.pb(fa[v][R]);
tmp_[sz] = fa[v][R];
tmp[sz++] = dp[v][R];
R++;
}
}
for ( auto &x: er ){
aux[x] = false;
}
for ( int j = 0; j < B; j++ ){
dp[v][j] = tmp[j];
fa[v][j] = tmp_[j];
}
}
}
vector <int> us(n);
while ( q-- ){
int t, y; cin >> t >> y;
--t;
vector <int> c(y);
for ( auto &x: c ){
cin >> x; --x;
us[x] = true;
}
if ( y + 1 < B ){
int opt = -1;
for ( int j = 0; j < B; j++ ){
if ( dp[t][j] != -1 && !us[fa[t][j]] ){
opt = dp[t][j];
break;
}
}
cout << opt << ln;
} else{
vector <int> dp(n, -1);
for ( int i = 0; i < n; i++ ){
if ( !us[i] ) chmax(dp[i], 0LL);
for ( auto &v: G[i] ){
if ( dp[i] != -1 ){
chmax(dp[v], dp[i] + 1);
}
}
}
cout << dp[t] << ln;
}
for ( auto &x: c ){
us[x] = false;
}
}
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |