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 v vector
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
const int mx = 1e5+5, sq = 100;
int n, m, q; vector<int> adj[mx]; v<pii> far[mx]; bool use[mx];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < m; i++){ int a, b; cin >> a >> b; adj[a-1].pb(b-1); }
auto merge = [&](v<pii> a, v<pii> b){
v<pii> ret; v<int> vis; int x = 0, y = 0, szA = a.size(), szB = b.size();
auto ins = [&](v<pii> &cur, int &i){
if (!use[cur[i].f]) ret.pb(cur[i]);
use[cur[i].f] = 1; vis.pb(cur[i].f); i++;
};while (ret.size() <= sq and (x < szA or y < szB)){
if (x < szA and (y >= szB or a[x].s+1 > b[y].s)) a[x].s++, ins(a, x);
else ins(b, y);
}for (auto i : vis){ use[i] = 0; } return ret;
};for (int i = 0; i < n; i++) far[i].pb({i, 0});
for (int i = 0; i < n; i++) for (int j : adj[i]) far[j] = merge(far[i], far[j]);
while (q--){
int t, y; cin >> t >> y; t--; int C[y], ans = -1; v<int> vis;
for (int &i : C) cin >> i, i--, use[i] = 1, vis.pb(i);
if (y <= sq){ for (auto i : far[t]) if (!use[i.f]){ ans = i.s; break; } }
else{
int dp[t+1]; memset(dp, -1, sizeof(dp));
for (int i = 0; i <= t; i++){
if (!use[i]) dp[i] = max(dp[i], 0);
for (int j : adj[i]) if (j <= t) dp[j] = max(dp[j], (dp[i] == -1) ? dp[i] : dp[i]+1);
}ans = dp[t];
}for (auto i : vis){ use[i] = 0; } cout<<ans<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |