This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)
#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)
#define NN 100010
#define M 1000000007 // 998244353
vector<ll> adj[NN];
ll n;
vector<ll> topo;
bool bad[NN];
ll dpfast[NN];
constexpr ll SN = 200;
vector<pl> dp[NN]; // only keep up to sqrt(N) things in here
int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(20);
cin >> n; G(m) G(q)
while (m--) {
G(s) G(e) adj[e].push_back(s);
}
{
bool vis[NN] = {};
vector<ll> temp;
auto dfs = [&](auto&& self, ll i) -> void {
if (vis[i]) return;
vis[i] = 1;
for (auto y: adj[i]) self(self, y);
temp.push_back(i);
};
F(i, 1, n+1) if (!vis[i]) {
temp.clear();
dfs(dfs, i);
topo.insert(topo.end(), temp.rbegin(), temp.rend());
}
}
for (auto x: topo) {
dp[x].emplace_back(0, x);
for (auto y: adj[x]) {
for (auto z: dp[y]) dp[x].emplace_back(z.K+1, z.V);
}
sort(A(dp[x]), greater());
while (dp[x].size() > SN) dp[x].pop_back();
}
while (q--) {
G(t) G(m)
vector<ll> v(m);
for (auto &x: v) {
cin >> x;
bad[x]=1;
}
if (m >= SN) {
memset(dpfast, 0, sizeof dpfast);
for (auto x: v) dpfast[x] = -1e9;
for (auto x: topo) {
for (auto y: adj[x]) dpfast[x] = max(dpfast[x], 1 + dpfast[y]);
}
cout << (dpfast[t] < 0 ? -1: dpfast[t]) << '\n';
} else {
ll ans = -1;
for (auto [dist, node]: dp[t]) if (!bad[node]) ans = max(ans, dist);
cout << ans << '\n';
}
for (auto x: v) bad[x] = 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... |