#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
constexpr int B = (int) sqrt(100'000);
int main() {
cin.tie(0) -> sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> adj(n);
vector<int> deg(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
++deg[v];
}
vector<vector<pii>> best(n);
auto cur = deg;
queue<int> top;
for (int i = 0; i < n; i++) {
if (!cur[i]) { top.push(i); }
}
const auto merge = [&](int from, int to) -> void {
map<int, int> mp;
for (auto [node, dist] : best[from]) {
mp[node] = max(mp[node], dist + 1);
}
for (auto [node, dist] : best[to]) {
mp[node] = max(mp[node], dist);
}
best[to].clear();
for (auto [node, dist] : mp) {
best[to].push_back({node, dist});
}
sort(all(best[to]), [&](auto &x, auto &y) -> bool {
return x[1] > y[1];
});
if (sz(best[to]) > B) { best[to].resize(B); }
};
while (!top.empty()) {
int u = top.front(); top.pop();
if (sz(best[u]) < B) {
best[u].push_back({u, 0});
}
for (int v : adj[u]) {
merge(u, v);
if (!(--cur[v])) { top.push(v); }
}
}
const auto check = [&](int t, int y, const set<int> &bad) -> int {
for (auto [node, dist] : best[t]) {
if (!bad.count(node)) {
return dist;
}
}
return -1;
};
const auto brute_force = [&](int t, int y, const set<int> &bad) -> int {
queue<pii> q;
auto cur = deg;
for (int i = 0; i < n; i++) {
if (!cur[i]) { q.push({i, -1}); }
}
while (!q.empty()) {
auto [u, dx] = q.front(); q.pop();
if (dx >= 0) { ++dx; }
else if (!bad.count(u)) { dx = 0; }
if (u == t) { return dx; }
for (int v : adj[u]) {
if (!(--cur[v])) {
q.push({v, dx});
}
}
}
return -1;
};
while (q--) {
int t, y;
cin >> t >> y;
--t;
set<int> bad;
for (int i = 0; i < y; i++) {
int x; cin >> x;
bad.insert(--x);
}
if (y >= B) {
cout << brute_force(t, y, bad) << "\n";
} else {
cout << check(t, y, bad) << "\n";
}
}
/*
for (int i = 0; i < n; i++) {
cout << "node " << i + 1 << "\n";
for (auto [node, dist] : best[i]) {
cout << node + 1 << " " << dist << "\n";
}
}
*/
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
1040 KB |
Output is correct |
6 |
Correct |
7 ms |
860 KB |
Output is correct |
7 |
Incorrect |
6 ms |
1272 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
1040 KB |
Output is correct |
6 |
Correct |
7 ms |
860 KB |
Output is correct |
7 |
Incorrect |
6 ms |
1272 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
1040 KB |
Output is correct |
6 |
Correct |
7 ms |
860 KB |
Output is correct |
7 |
Incorrect |
6 ms |
1272 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |