This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define task "B"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e5 + 2, SQ = 400;
int n, m, q, na, mx[N], dp[N];
ii a[N];
vi g[N];
vii data[N];
void preCal()
{
rep(i, 1, n)
{
na = 0;
vi change;
for (int v : g[i])
{
if (!mx[v]) change.eb(v);
mx[v] = max(mx[v], 1);
for (ii it : data[v])
{
if (!mx[it.S]) change.eb(it.S);
mx[it.S] = max(mx[it.S], it.F + 1);
}
}
na = 0;
for (int v : change) a[++na] = {mx[v], v}, mx[v] = 0;
nth_element(a + 1, a + 1 + min(SQ, na), a + 1 + na, [&](const ii &A, const ii &B)
{
return A.F > B.F;
});
data[i].assign(min(na, SQ), {0, 0});
rep(j, 0, min(na, SQ) - 1) data[i][j] = a[j + 1];
}
}
bool in[N];
int calDP(int nn)
{
rep(i, 1, nn)
{
dp[i] = 0;
for (int v : g[i]) if (!in[v] || dp[v]) dp[i] = max(dp[i], dp[v] + 1);
}
if (in[nn] && dp[nn] == 0) return -1;
return dp[nn];
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n >> m >> q;
rep(i, 1, m)
{
int u, v;
cin >> u >> v;
g[v].eb(u);
}
preCal();
while (q--)
{
int u, num;
cin >> u >> num;
vi change(num, 0);
rep(i, 0, num - 1) cin >> change[i], in[change[i]] = 1;
if (num >= SQ) cout << calDP(u) << '\n';
else
{
int mxx = -1;
for (ii it : data[u]) if (!in[it.S]) mxx = max(mxx, it.F);
if (!in[u]) mxx = max(mxx, 0);
cout << mxx << '\n';
}
rep(i, 0, num - 1) in[change[i]] = 0;
}
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... |