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;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 1e5 + 100;
const int block = 300;
vector <int> ed[N];
vector <pair <int, int>> dp[N];
int mx[N];
void dfs(int u) {
if (dp[u].size())
return;
vector <int> lst;
lst.pb(u);
mx[u] = 0;
for (auto x : ed[u]) {
dfs(x);
for (auto t : dp[x]) {
if (mx[t.s] == -1)
lst.pb(t.s);
mx[t.s] = max(mx[t.s], t.f + 1);
}
}
for (auto x : lst) {
dp[u].pb({mx[x], x});
mx[x] = -1;
}
sort(all(dp[u]), greater <pair <int, int>> ());
while (sz(dp[u]) > block)
dp[u].pop_back();
}
int f[N], busy[N], used[N];
ll cal(int u) {
if (used[u])
return f[u];
f[u] = -1;
if (busy[u] == 0)
f[u] = 0;
for (auto x : ed[u]) {
int nxt = cal(x);
if (nxt == -1)
continue;
f[u] = max(f[u], nxt + 1);
}
used[u] = 1;
return f[u];
}
ll solve() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
ed[b].pb(a);
}
for (int i = 1; i <= n; i++) {
if (dp[i].empty()) {
dfs(i);
}
}
for (int i = 1; i <= q; i++) {
int t, y;
cin >> t >> y;
vector <int> lst(y);
for (auto &x : lst) {
cin >> x;
busy[x] = 1;
}
if (y >= block) {
mem(used, 0);
cout << cal(t) << '\n';
} else {
int ans = -1;
for (auto x : dp[t]) {
if (busy[x.s] == 0) {
ans = x.f;
break;
}
}
cout << ans << '\n';
}
for (auto x : lst) {
busy[x] = 0;
}
}
return 0;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
mem(mx, -1);
int t = 1;
//cin >> t;
while (t--) {
solve();
//cout << solve() << '\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... |