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<vector<int>> adj, vraiadj;
vector<vector<pair<vector<int>, int>>> queries;
vector<int> ans;
vector<vector<pair<int, int>>> memo;
vector<int> memo2;
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++;
unordered_map<int, int> res;
vector<pair<int, int>> temp;
int cnt=0;
while (cnt++<(int)(mx*1.5) && sz(temp)<mx) {
pair<int, int> best={-1, -1};
for (auto x: vec) if (!x.empty()) {
if (best.fi<x.back().fi) best=x.back();
}
if (best.fi==-1) break;
for (auto &x: vec) if (!x.empty()) {
if (x.back()==best) x.pop_back();
}
res[best.se]=best.fi;
temp.pb(best);
}
reverse(all(temp));
return memo[u]=temp;
}
void solve() {
read(n, m, q);
SQRT=sqrt(n)-100;
queries.resize(n), ans.resize(q), adj.resize(n), vraiadj.resize(n), memo.resize(n);
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;
memo2.clear(), memo2.resize(x+1, -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);
reverse(all(temp));
for (auto [a, j]: queries[i]) {
ans[j]=calc(a, temp);
}
}
for (auto u: ans) cout << u << 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... |