이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MAXS = 100;
vector<int> adj[MAXN], rev[MAXN];
vector<pair<int, int>> caras[MAXN];
int in[MAXN], bosta[MAXN], best[MAXN], dp[MAXN], valid[MAXN];
bool mark[MAXN], blocked[MAXN];
void merge(int a, int b) {
vector<pair<int, int>> result;
int j = 0;
for (int i = 0; i < (int) caras[a].size(); i++) {
for (; j < (int) caras[b].size() && caras[b][j].first + 1 > caras[a][i].first; j++) {
result.push_back({caras[b][j].first + 1, caras[b][j].second});
}
result.push_back(caras[a][i]);
}
for (; j < (int) caras[b].size(); j++) {
result.push_back({caras[b][j].first + 1, caras[b][j].second});
}
caras[a].clear();
for (auto x : result) {
if (!mark[x.second] && (int) caras[a].size() < MAXS) {
caras[a].push_back(x);
mark[x.second] = true;
}
}
for (auto x : caras[a]) {
mark[x.second] = false;
}
}
int solve(int v) {
if (dp[v] != -1) return dp[v];
valid[v] = (int) !blocked[v];
int ans = 0;
for (int u : rev[v]) {
ans = max(ans, solve(u) + valid[u]);
if (valid[u]) valid[v] = 1;
}
return dp[v] = ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
rev[b].push_back(a);
in[b]++;
}
vector<int> lista;
for (int i = 1; i <= n; i++) {
caras[i].push_back({0, i});
if (!in[i]) lista.push_back(i);
}
for (int i = 0; i < (int) lista.size(); i++) {
int cur = lista[i];
for (int nxt : adj[cur]) {
in[nxt]--;
if (!in[nxt]) lista.push_back(nxt);
merge(nxt, cur);
}
}
for (int i = 0; i < q; i++) {
int t, y;
cin >> t >> y;
for (int j = 0; j < y; j++) {
cin >> bosta[j];
blocked[bosta[j]] = true;
}
if (y >= MAXS) {
memset(dp, -1, sizeof dp);
memset(valid, 0, sizeof valid);
int ans = (solve(t) == 0 ? blocked[t] ? -1 : 0 : dp[t]);
cout << ans << '\n';
} else {
int ans = -1;
for (auto x : caras[t]) {
if (!blocked[x.second]) {
ans = x.first;
break;
}
}
cout << ans << '\n';
}
for (int j = 0; j < y; j++) {
blocked[bosta[j]] = false;
}
}
return 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... |