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 endl '\n'
#define int long long
typedef long long ll;
const int M = 1e5 + 5, MOD = 1e9+7;
vector<int> node[M], inv[M], top;
int vis[M], in[M];
map<int, int> is;
void dfs1(int s) {
vis[s] = true;
for (int i:node[s]) if (!vis[i]) dfs1(i);
top.push_back(s);
}
void dfs2(int s) {
vis[s] = true;
for (int i:inv[s]) if (!vis[i]) dfs2(i);
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
srand(time(0));
int n, m, t;
cin >> n >> m >> t;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
node[a].push_back(b);
inv[b].push_back(a);
}
for (int i = 1; i <= n; i++) if (!vis[i]) dfs1(i);
reverse(top.begin(), top.end());
for (int i = 0; i < n; i++) in[top[i]] = i;
while (t--) {
int s, k;
cin >> s >> k;
for (int i = 1; i <= k; i++) {
int a;
cin >> a;
is[a] = true;
}
for (int i = 1; i <= n; i++) vis[i] = 0;
dfs2(s);
vector<int> dp(n+1, 0);
for (int i = in[s]; i >= 0; i--) {
int x = top[i];
if (!vis[x]) continue;
for (int j:inv[x]) {
dp[j] = max(dp[x]+1, dp[j]);
}
}
int ans = -1;
for (int i = 1; i <= n; i++) {
if (!is[i] && vis[i]) {
//cout << i << ' ';
ans = max(ans, dp[i]);
}
} //cout << ": ";
cout << ans << endl;
is.clear();
}
return 0;
}
/*
5 6 3
1 2
2 4
3 4
1 3
3 5
4 5
4 1 1
5 2 2 3
2 3 1 4 5
12 17 10
1 2
2 3
3 4
1 5
2 6
3 7
4 8
5 6
6 7
7 8
5 9
6 10
7 11
8 12
9 10
10 11
11 12
6 3 1 7 12
3 7 1 2 3 4 5 6 7
11 3 1 3 5
9 2 1 9
8 4 1 2 3 4
1 1 1
12 0
10 3 1 6 10
11 8 2 3 5 6 7 9 10 11
8 7 2 3 4 5 6 7 8
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |