This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/JOI18_bitaro
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5 + 100;
const int sqrtn = 320;
int n, m, q;
vector<int> adj[maxn];
vector<int> readj[maxn];
vector<pii> fv[maxn];
bool mark[maxn];
int main () {
ios_base::sync_with_stdio(false);
cin >> n >> m >> q;
for (int i = 1; i <= m; i++) {
int s, e;
cin >> s >> e;
adj[s].push_back(e);
readj[e].push_back(s);
}
for (int i = 1; i <= n; i++) {
vector<pii> tmp;
tmp.push_back({0, i});
for (int j : readj[i]) {
for (auto k : fv[j]) {
tmp.push_back({k.first+1, k.second});
}
}
sort(tmp.begin(), tmp.end(), greater<pii>());
for (int j = 0; j < min(int(tmp.size()), sqrtn); j++) {
fv[i].push_back({tmp[j].first, tmp[j].second});
}
}
for (int i = 1; i <= q; i++) {
int t, y;
cin >> t >> y;
if (y < sqrtn) {
vector<int> tmp;
for (int i = 1; i <= y; i++) {
int c;
cin >> c;
tmp.push_back(c);
}
sort(tmp.begin(), tmp.end());
bool is_find = false;
for (pii i : fv[t]) {
if (!binary_search(tmp.begin(), tmp.end(), i.second)) {
cout << i.first << endl;
is_find = true;
break;
}
}
if (!is_find) {
cout << -1 << endl;
}
} else {
for (int i = 1; i <= n; i++) {
mark[i] = false;
}
for (int i = 1; i <= y; i++) {
int c;
cin >> c;
mark[c] = true;
}
vector<int> dis(t+2, -1);
int maximum = -1;
if (!mark[t]) {
dis[t] = 0;
maximum = 0;
}
for (int i = t-1; i >= 1; i--) {
if (mark[i]) {
dis[i] = -1;
continue;
}
for (int j : adj[i]) {
if (j > t) {
continue;
}
dis[i] = max(dis[i], dis[j] +1);
}
maximum = max(maximum, dis[i]);
}
cout << maximum << endl;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |