# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
881273 | bobbilyking | Bitaro’s Party (JOI18_bitaro) | C++17 | 0 ms | 0 KiB |
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 dp[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(dp, 0, sizeof dp);
for (auto x: v) dp[x] = -1e9;
for (auto x: topo) {
for (auto y: adj[x]) dp[x] = max(dp[x], 1 + dp[y]);
}
cout << (dp[t] < 0 ? -1: dp[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;
}
}