이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
bool have[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 = 200;
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);
}
}
for (int i = (int)ts.size() - 1; i >= 0; --i) {
int u = ts[i];
int sz = rg[u].size();
vector <int> uk(sz);
while (true) {
pair <int, int> mx = {0, u};
int id = -1;
for (int i = 0; i < sz; ++i) {
while (uk[i] < (int)best[rg[u][i]].size() && have[best[rg[u][i]][uk[i]].second]) {
++uk[i];
}
if (uk[i] < (int)best[rg[u][i]].size()) {
auto cp = best[rg[u][i]][uk[i]];
++cp.first;
if (cp > mx) {
mx = cp;
id = i;
}
}
}
if (id == -1) {
best[u].push_back(mx);
break;
} else {
best[u].push_back(mx);
++uk[id];
have[mx.second] = 1;
if ((int)best[u].size() == K) break;
}
}
for (int v : rg[u]) {
for (auto p : best[v]) {
have[p.second] = 0;
}
}
}
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... |