This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
* Author: Nonoze
* Created: Monday 22/07/2024
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef _IN_LOCAL
#define dbg(...)
#endif
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)
template<typename T> void read(T& x, bool write=0, bool write2=1) { if (write && write2) cout << x << endl; else if (write) cout << x << ' '; else cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p, bool write=0, bool write2=1) { read(p.first, write, 0), read(p.second, write, 0); if (write && write2) cout << endl;}
template<typename T> void read(vector<T>& v, bool write=0) { for (auto& x : v) read(x, write, 0); if (write) cout << endl; }
template<typename T1, typename T2> void read(T1& x, T2& y, bool write=0) { read(x, write), read(y, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z, bool write=0) { read(x, write), read(y, write), read(z, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz, bool write=0) { read(x, write), read(y, write), read(z, write), read(zz, write); if (write) cout << endl; }
template<typename T> void print(T x) { read(x, 1); }
template<typename T1, typename T2> void print(T1 x, T2 y) { read(x, y, 1); }
template<typename T1, typename T2, typename T3> void print(T1 x, T2 y, T3 z) { read(x, y, z, 1); }
template<typename T1, typename T2, typename T3, typename T4> void print(T1 x, T2 y, T3 z, T4 zz) { read(x, y, z, zz, 1); }
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
// #define int long long
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;
void solve();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
// cin >> tt;
while(tt--) solve();
return 0;
}
int SQRT;
int n, k, m, q;
vector<int> adj[100000], vraiadj[100000];
vector<pair<vector<int>, int>> queries[100000];
vector<int> ans;
vector<pair<int, int>> memo[100000];
int memo2[100000];
int mx=1;
int dp2(int u, int goal) {
if (u>goal) return -inf;
if (u == goal) return 0;
if (memo2[u] != -1) return memo2[u];
int res = -inf;
for (int v : vraiadj[u]) {
cmax(res, dp2(v, goal) + 1);
}
return memo2[u] = res;
}
int calc(vector<int> &busy, vector<pair<int, int>>& s) {
unordered_set<int> busy2;
for (auto u: busy) busy2.insert(u);
for (auto u: s) {
if (!busy2.count(u.se)) return u.fi;
}
return -1;
}
vector<pair<int, int>> dp(int u) {
if (!memo[u].empty()) return memo[u];
vector<vector<pair<int, int>>> vec; vec.pb({{-1, u}});
for (auto v: adj[u]) vec.pb(dp(v));
for (auto &x: vec) for (auto &y: x) y.fi++;
vector<int> empl(sz(vec), 0);
vector<bool> used(u+1, 0);
vector<pair<int, int>> temp;
int cnt=0;
while (sz(temp)<mx) {
pair<int, int> best={-1, -1};
int pos=0;
for (int i=0; i<sz(vec); i++) {
while (empl[i]<sz(vec[i]) && used[vec[i][empl[i]].se]) empl[i]++;
if (empl[i]<sz(vec[i]) && best.fi<vec[i][empl[i]].fi) best=vec[i][empl[i]], pos=i;
}
if (best.fi==-1) break;
empl[pos]++;
used[best.se]=1;
temp.pb(best);
}
return memo[u]=temp;
}
void solve() {
read(n, m, q);
SQRT=380;
ans.resize(q);
for (int i=0; i<m; i++) {
int u, v; cin >> u >> v; u--, v--;
adj[v].pb(u);
vraiadj[u].pb(v);
}
for (int i=0; i<q; i++) {
int x, y; cin >> x >> y; x--;
vector<int> a(y);
for (int j=0; j<y; j++) cin >> a[j], a[j]--;
if (y>SQRT) {
vector<bool> busy(n, 0);
for (auto u: a) busy[u]=1;
ans[i]=-1;
for (int i=0; i<=x; i++) memo2[i]=-1;
for (int j=0; j<=x; j++) {
if (!busy[j]) cmax(ans[i], dp2(j, x));
}
}
else cmax(mx, sz(a)+1), queries[x].push_back({a, i});
}
for (int i=n-1; i>=0; i--) {
if (queries[i].empty()) continue;
vector<pair<int, int>> temp=dp(i);
for (auto [a, j]: queries[i]) {
ans[j]=calc(a, temp);
}
}
for (auto u: ans) cout << u << endl;
}
Compilation message (stderr)
bitaro.cpp: In function 'std::vector<std::pair<int, int> > dp(int)':
bitaro.cpp:96:6: warning: unused variable 'cnt' [-Wunused-variable]
96 | int cnt=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... |