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;
typedef long double ld;
const int N = 1e5 + 7;
const int Inf = 1e9;
vector <int> g[N];
vector <int> rg[N];
bool used[N];
int dp[N];
int ban[N];
vector <pair <int, int>> best[N];
vector <int> ts;
void dfs(int u) {
used[u] = 1;
for (int v : g[u]) {
if (!used[v]) {
dfs(v);
}
}
ts.push_back(u);
}
const int K = 400;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
rg[v].push_back(u);
}
for (int i = 0; i < n; ++i) {
if (!used[i]) {
dfs(i);
}
}
vector <pair <int, int>> tm;
vector <pair <int, int>> df;
for (int i = (int)ts.size() - 1; i >= 0; --i) {
int u = ts[i];
for (int v : rg[u]) {
for (auto p : best[v]) {
tm.push_back({p.second, p.first + 1});
}
}
tm.push_back({u, 0});
sort(tm.begin(), tm.end());
for (int i = 1; i < (int)tm.size(); ++i) {
if (tm[i].first != tm[i - 1].first) {
df.push_back({tm[i - 1].second, tm[i - 1].first});
}
}
df.push_back({tm.back().second, tm.back().first});
sort(df.rbegin(), df.rend());
for (int i = 0; i < min(K, (int)df.size()); ++i) best[u].push_back(df[i]);
tm.clear();
df.clear();
}
while (q--) {
int t;
cin >> t;
--t;
int y;
cin >> y;
vector <int> who;
for (int i = 0; i < y; ++i) {
int x;
cin >> x;
--x;
who.push_back(x);
ban[x] = 1;
}
if (y >= K) {
fill(dp, dp + n, -Inf);
dp[t] = 0;
for (int x : ts) {
for (int v : g[x]) {
dp[x] = max(dp[x], dp[v] + 1);
}
}
int ans = -1;
for (int i = 0; i < n; ++i) if (!ban[i]) ans = max(ans, dp[i]);
cout << ans << '\n';
} else {
int ans = -1;
for (auto p : best[t]) {
if (!ban[p.second]) {
ans = max(ans, p.first);
}
}
cout << ans << '\n';
}
for (int x : who) ban[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... |