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"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?b:gcd(b%a,a); }
#define ll /*long long*/ int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (100006)
#define MAXE (200006)
const ll sqr=305;
ll n,m,q;
int dp[MAXN];
vector<int> v[MAXN];
bitset<MAXN> vis;
vector<int> topo, Q1, Q2, dest;
vector<vector<int>> busy;
vector<ll> ans;
void dfs(int x) {
if(vis[x]) return;
vis[x]=1;
for(auto i:v[x]) dfs(i);
topo.pb(x);
}
void solve2(){
vector<vector<pair<int,int>>> dp; vector<pair<int,int>> mx_val(n+1, {-INF, 0}); int co = 1;
dp.resize(n+1, vector<pair<int,int>>(sqr, {-INF,-1}));
FOR(i,1,n) dp[i][0]={0, i};
for(auto i:topo) {
for(auto j:v[i]) {
if(dp[i][0].f+1 <= dp[j].back().f) continue;
vector<pair<int,int>> vals;
vector<int> ind, ign1, ign2;
FOR(i,0,sqr-1) if(dp[j][i].s!=-1) ind.pb(dp[j][i].s), mx_val[dp[j][i].s] = {dp[j][i].f, co}; else break;
ign1.resize(ind.size(), 1);
ign2.resize(dp[i].size(), 1);
FOR(j,0,sqr-1) if(dp[i][j].s!=-1) {
if(mx_val[dp[i][j].s].s != co);// vals.eb(dp[i][j].f+1, dp[i][j].s);
else if(mx_val[dp[i][j].s].f>=dp[i][j].f+1) ign2[j]=0;
else mx_val[dp[i][j].s].f=dp[i][j].f+1;
} else break;
FOR(i,0,siz(ind)-1) if(mx_val[ind[i]].f != dp[j][i].f) ign1[i]=0;
ll co1=0, co2=0;
while(co1<ind.size() || co2<dp[i].size()) {
if(co1 < ind.size() && ign1[co1]==0) { ++ co1; continue; }
if(dp[i][co2].s == -1) { co2=dp[i].size(); continue; }
if(co2 < dp[i].size() && ign2[co2] == 0) { ++ co2; continue; }
if(co1==ind.size()) vals.eb(dp[i][co2].f+1, dp[i][co2].s), ++co2;
else if(co2==dp[i].size()) assert(ind[co1]==dp[j][co1].s), vals.eb(dp[j][co1].f, dp[j][co1].s), ++ co1;
else if(dp[j][co1].f >= dp[i][co2].f+1) assert(ind[co1]==dp[j][co1].s), vals.eb(dp[j][co1].f, dp[j][co1].s), ++co1;
else vals.eb(dp[i][co2].f+1, dp[i][co2].s), ++co2;
}
FOR(i,0,sqr-1) if(vals.size()<=i) dp[j][i]={-INF, -1}; else dp[j][i]=vals[i];
++ co;
}
}
vis.set();
for(auto i:Q2) {
for(auto j:busy[i]) vis[j]=0;
for(auto j:dp[dest[i]]) if(j.s != -1 && vis[j.s]) {
ans[i]=j.f; break;
}
for(auto j:busy[i]) vis[j]=1;
}
}
#define gc getchar_unlocked
void in(ll &x) {
x=0;
char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch>='0'&&ch<='9') {
x=(x<<3)+(x<<1)+ch-48;
ch=gc();
}
}
int main()
{
FAST
in(n), in(m), in(q); ans.resize(q, -1);
FOR(i,0,m-1) {
ll s, e; in(s),in(e);
v[s].eb(e);
}
FOR(i,0,q-1) {
busy.pb(vector<int>());
ll t, y; in(t),in(y); dest.pb(t); busy.back().resize(y, 0);
FOR(i,0,y-1) in(busy.back()[i]);
if(y >= sqr) Q1.pb(i); else Q2.pb(i);
}
FOR(i,1,n) dfs(i);
reverse(all(topo));
vis.set();
for(auto i:Q1) {
FOR(i,1,n) dp[i]=-1e9;
for(auto j:busy[i]) vis[j]=0;
for(auto i:topo) {
if(vis[i]) dp[i]=max(dp[i], 0);
for(auto j:v[i]) {
dp[j]=max(dp[j], dp[i]+1);
}
}
if(dp[dest[i]] >= 0) ans[i]=dp[dest[i]];
for(auto j:busy[i]) vis[j]=1;
}
solve2();
FOR(i,0,q-1) {
cout<<(ans[i]<0?-1:ans[i])<<'\n';
}
}
Compilation message (stderr)
bitaro.cpp: In function 'void solve2()':
bitaro.cpp:66:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(co1<ind.size() || co2<dp[i].size()) {
~~~^~~~~~~~~~~
bitaro.cpp:66:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(co1<ind.size() || co2<dp[i].size()) {
~~~^~~~~~~~~~~~~
bitaro.cpp:67:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(co1 < ind.size() && ign1[co1]==0) { ++ co1; continue; }
~~~~^~~~~~~~~~~~
bitaro.cpp:69:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(co2 < dp[i].size() && ign2[co2] == 0) { ++ co2; continue; }
~~~~^~~~~~~~~~~~~~
bitaro.cpp:70:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(co1==ind.size()) vals.eb(dp[i][co2].f+1, dp[i][co2].s), ++co2;
~~~^~~~~~~~~~~~
bitaro.cpp:71:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
else if(co2==dp[i].size()) assert(ind[co1]==dp[j][co1].s), vals.eb(dp[j][co1].f, dp[j][co1].s), ++ co1;
~~~^~~~~~~~~~~~~~
bitaro.cpp:75:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
FOR(i,0,sqr-1) if(vals.size()<=i) dp[j][i]={-INF, -1}; else dp[j][i]=vals[i];
~~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |