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>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 1e5 + 7;
vi G[N]; // from
V<pi> who[N];
int vis[N];
int n, m, q, k;
void dfs(int u) {
unordered_map<int, int> mn;
vis[u] = 1;
who[u].EB(0, u);
mn[u] = 0;
for(int v:G[u]) {
if(!vis[v]) dfs(v);
V<pi> tt = who[v];
for(pi& i:tt) i.F--;
V<pi> ttt, tttt;
for(pi p:tt) {
if(mn.count(p.S) && p.F >= mn[p.S])
continue;
mn[p.S] = p.F;
ttt.PB(p);
}
for(pi p:who[u]) {
if(mn[p.S] == p.F) tttt.PB(p);
}
who[u].resize(ttt.size() + tttt.size());
merge(ALL(tttt), ALL(ttt), who[u].begin());
if(int(who[u].size()) > k) who[u].resize(k);
}
}
int no[N];
signed main()
{
IO_OP;
cin >> n >> m >> q;
k = 100;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[v].PB(u);
}
for(int i = 1; i <= n; i++)
if(!vis[i])
dfs(i);
while(q--) {
int u, y;
cin >> u >> y;
vi x(y);
for(int i = 0; i < y; i++) {
cin >> x[i];
no[x[i]] = 1;
}
if(y >= k) {
vi vis(n + 1), dp(n + 1);
function<int(int)> DP = [&] (int u) {
if(vis[u]) return dp[u];
vis[u] = 1;
if(no[u]) dp[u] = -INF;
else dp[u] = 0;
for(int v:G[u]) {
dp[u] = max(dp[u], DP(v) + 1);
}
return dp[u];
};
cout << max(DP(u), -1) << '\n';
} else {
int ans = -1;
for(pi p:who[u]) {
if(no[p.S] == 0) {
ans = -p.F;
break;
}
}
cout << ans << '\n';
}
for(int i = 0; i < y; i++) no[x[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... |