이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In The Name Of The God
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define rep(a, b, c) for (int (a) = (b); (a) <= (c); a++)
#define per(a, b, c) for (int (a) = (b); (a) >= (c); a--)
#define nl '\n'
#define ioi exit(0);
using namespace std;
const int MAX_N = (int)1e5 + 7;
const int K = 100;
const int inf = (int)1e9 + 7;
int n, m, q;
list <int> g[MAX_N];
int T, k;
int tmr;
int a[MAX_N], was[MAX_N], dp[MAX_N], h[MAX_N];
bool ban[MAX_N];
int calc(int v) {
int res = -inf;
if (was[v] == tmr) return dp[v];
was[v] = tmr;
if (!ban[v]) res = 0;
for (auto to : g[v]) {
res = max(res, calc(to) + 1);
}
return dp[v] = res;
}
void read() {
cin >> T >> k;
rep(i, 1, k) {
cin >> a[i];
}
}
void naive() {
rep(i, 1, k) {
ban[a[i]] = 1;
}
++tmr;
int res = calc(T);
if (res < 0) res = -1;
cout << res << nl;
rep(i, 1, k) {
ban[a[i]] = 0;
}
}
int sz[MAX_N], used[MAX_N], pos[MAX_N];
pair <int, int> bfr[MAX_N];
pair <int, int> dist[MAX_N][K];
void pre() {
rep(i, 1, n) {
int S = 0;
bfr[S++] = {0, i};
for (auto to : g[i]) {
for (int j = 0; j < sz[to]; j++) {
auto it = dist[to][j];
if (used[it.s] == i) {
bfr[pos[it.s]].f = max(bfr[pos[it.s]].f, it.f + 1);
continue;
}
pos[it.s] = S;
used[it.s] = i;
bfr[S++] = {it.f + 1, it.s};
}
}
sort(bfr, bfr + S, greater < pair <int, int> > () );
rep(j, 0, S - 1) {
dist[i][sz[i]++] = bfr[j];
if (sz[i] == K) break;
}
}
}
void fast() {
int res = -1;
rep(i, 1, k) {
ban[a[i]] = 1;
}
rep(i, 0, sz[T] - 1) {
if (!ban[dist[T][i].s]) {
res = max(res, dist[T][i].f);
break;
}
}
rep(i, 1, k) {
ban[a[i]] = 0;
}
cout << res << nl;
//cerr << "DA\n";
}
int main() {
ios_base :: sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#ifdef IOI
freopen ("in.txt", "r", stdin);
freopen ("B.out", "w", stdout);
#endif
cin >> n >> m >> q;
rep(i, 1, m) {
int s, e;
cin >> s >> e;
g[e].pb(s);
}
pre();
rep(i, 1, q) {
read();
if (k >= K) naive();
else fast();
}
ioi
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |