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 pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 2e5 + 10, K = 400;
int n, m, q;
veci g[MAXN];
veci gr[MAXN];
vector <pii> dp[MAXN];
int d[MAXN];
bool was[MAXN];
bool cmp(const int &x, const int &y) {
return (d[x] > d[y]);
}
void solve() {
cin >> n >> m >> q;
for(int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
g[u].pb(v);
gr[v].pb(u);
}
for(int i = 1; i <= n; ++i) {
veci cur;
cur.pb(i);
for(auto to : gr[i]) {
for(auto it : dp[to]) {
if(!d[it.f]) cur.pb(it.f);
d[it.f] = max(d[it.f], it.s + 1);
}
}
sort(all(cur), &cmp);
for(int j = 0; j < min(sz(cur), K + 1); ++j) dp[i].pb({cur[j], d[cur[j]]});
for(auto it : cur) d[it] = 0;
/**cout << i << ": ";
for(auto it : dp[i]) cout << it.f << ' ' << it.s << '\n';
cout << '\n';**/
}
for(int FUK = 1; FUK <= q; ++FUK) {
int v, y;
cin >> v >> y;
veci bl;
for(int j = 1, u; j <= y; ++j) {
cin >> u;
bl.pb(u);
was[u] = 1;
}
int ans = -1;
if(!was[v]) ans = 0;
if(y < K) {
for(auto it : dp[v]) if(!was[it.f]) ans = max(ans, it.s);
} else {
for(int j = 1; j <= v; ++j) {
d[j] = 0;
if(was[j]) d[j] = INT_MIN;
for(auto it : gr[j]) d[j] = max(d[j], d[it] + 1);
}
ans = max(ans, d[v]);
}
cout << ans << '\n';
for(auto it : bl) was[it] = 0;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int CNT_TESTS = 1;
///cin >> CNT_TESTS;
for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) {
solve();
if(NUMCASE != CNT_TESTS) cout << '\n';
}
return 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... |