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 = 250;
const int inf = 1e9;
int n, m, q;
vector<int> adj[maxn];
vector<int> readj[maxn];
vector<pii> fv[maxn];
int pointer[maxn];
int dis[maxn];
bool mark[maxn];
bool mark2[maxn];
bool mark3[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);
}
vector<int> tmp1;
for (int i = 1; i <= n; i++) {
tmp1.clear();
for (int w = 0; w <= sqrtn; w++) {
int maximum = -1;
int v = -1;
int nv = -1;
for (int j : readj[i]) {
while (int(fv[j].size()) > pointer[j]) {
if (mark2[fv[j][pointer[j]].second]) {
pointer[j]++;
continue;
}
if (fv[j][pointer[j]].first +1 > maximum) {
maximum = fv[j][pointer[j]].first +1;
v = fv[j][pointer[j]].second;
nv = j;
}
break;
}
}
if (v == -1)
break;
mark2[v] = true;
tmp1.push_back(v);
fv[i].push_back({maximum, v});
pointer[nv]++;
}
for (int j : readj[i])
pointer[j] = 0;
for (int j : tmp1) {
mark2[j] = false;
}
fv[i].push_back({0, i});
}
/*for (int i = 1; i <= n; i++) {
fv[i].push_back(pii(0, i));
for (auto &j : readj[i]) {
vector<pii> nxt = fv[j];
vector<pii> prv = fv[i];
fv[i].clear();
int ptr = 0;
for (auto &k : nxt) {
k.first++;
while (ptr < prv.size() && prv[ptr].first >= k.first) {
if (mark3[prv[ptr].second]) {
ptr++;
continue;
}
mark3[prv[ptr].second] = 1;
fv[i].push_back(prv[ptr++]);
}
if (mark3[k.second]) {
continue;
}
mark3[k.second] = 1;
fv[i].push_back(k);
}
while (ptr < prv.size()) {
if (mark3[prv[ptr].second]) {
ptr++;
continue;
}
mark3[prv[ptr].second] = 1;
fv[i].push_back(prv[ptr++]);
}
for (auto &i : nxt) {
mark3[i.second] = 0;
}
for (auto &i : prv) {
mark3[i.second] = 0;
}
while (fv[i].size() > sqrtn) {
fv[i].pop_back();
}
}
}*/
vector<int> tmp;
for (int _ = 1; _ <= q; _++) {
int t, y;
cin >> t >> y;
tmp.clear();
for (int i = 0; i < y; i++) {
int c;
cin >> c;
tmp.push_back(c);
mark[c] = true;
}
if (y < sqrtn) {
bool is_find = false;
for (pii i : fv[t]) {
if (!mark[i.second]) {
cout << i.first << endl;
is_find = true;
break;
}
}
if (!is_find) {
cout << -1 << endl;
}
} else {
fill(dis, dis+n+2, -1 * inf);
dis[t] = 0;
for (int i = t; i >= 1; i--) {
for (int j : readj[i]) {
dis[j] = max(dis[j], dis[i]+1);
}
}
int ans = -1;
for (int i= 1; i <= t; i++) {
if (!mark[i]) {
ans = max(ans, dis[i]);
}
}
cout << ans << endl;
}
for (int i : tmp) {
mark[i] = false;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |