# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
742510 | bgnbvnbv | Bitaro’s Party (JOI18_bitaro) | C++14 | 1112 ms | 414092 KiB |
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 TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=int;
const ll maxN=1e5+10;
const ll inf=1e8;
const ll mod=1e9+7;
const ll block=320;
ll t,y;
ll dp[maxN];
ll a[maxN];
vector<ll>g[maxN];
vector<pli>vec[maxN];
void brute()
{
for(int i=1;i<=t;i++) dp[i]=0;
for(int i=1;i<=y;i++) dp[a[i]]=-inf;
for(int i=1;i<=t;i++)
{
for(auto zz:g[i])
{
dp[i]=max(dp[i],dp[zz]+1);
}
}
if(dp[t]>=0) cout << dp[t]<<'\n';
else cout << -1<<'\n';
}
vector<pli> cc;
ll n;
bool vis[maxN];
void prepare()
{
for(int i=1;i<=n;i++)
{
vec[i].pb({i,0});
for(auto j:g[i])
{
cc.clear();
ll k=0;
ll q=0;
while(cc.size()<block)
{
if(k==vec[i].size()&&q==vec[j].size()) break;
else if(k==vec[i].size())
{
if(vis[vec[j][q].fi]==false)
{
cc.pb({vec[j][q].fi,vec[j][q].se+1});
vis[vec[j][q].fi]=true;
}
q++;
}
else if(q==vec[j].size())
{
if(vis[vec[i][k].fi]==false)
{
cc.pb({vec[i][k]});
vis[vec[i][k].fi]=true;
}
k++;
}
else if(vec[i][k].se>vec[j][q].se+1)
{
if(vis[vec[i][k].fi]==false)
{
cc.pb({vec[i][k]});
vis[vec[i][k].fi]=true;
}
k++;
}
else
{
if(vis[vec[j][q].fi]==false)
{
cc.pb({vec[j][q].fi,vec[j][q].se+1});
vis[vec[j][q].fi]=true;
}
q++;
}
}
swap(vec[i],cc);
for(auto zz:vec[i])
{
vis[zz.fi]=false;
}
}
}
}
bool valid[maxN];
ll m,q;
void solve()
{
cin >> n >> m >> q;
for(int i=1;i<=m;i++)
{
ll u,v;
cin >> u >> v;
g[v].pb(u);
}
for(int i=1;i<=n;i++) valid[i]=true;
prepare();
for(int i=1;i<=q;i++)
{
cin >> t >> y;
for(int j=1;j<=y;j++) cin >> a[j];
if(y>=block) brute();
else
{
for(int j=1;j<=y;j++) valid[a[j]]=false;
bool ok=true;
for(auto zz:vec[t])
{
if(valid[zz.fi]==true)
{
ok=false;
cout << zz.se<<'\n';
break;
}
}
if(ok==true)
{
cout << -1<<'\n';
}
for(int j=1;j<=y;j++) valid[a[j]]=true;
}
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |