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 ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 250;
const int mod = 1e9+7;
const ll base = 75;
const int inf = 1e9;
int m, n, t, b[N], d[N], lab[N], q;
int k, a[N];
pii dp[N][M], cur[M];
int ans, tong;
vector<int> adj[N], g[N];
vector<int> vi;
string s[N];
bool vis[N];
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n&1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
void add(int &x, int y)
{
x += y;
if(x >= mod)x -= mod;
}
int findp(int u)
{
return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
void hop(int u, int v)
{
u = findp(u);
v = findp(v);
if(u == v)return;
if(lab[u] > lab[v])swap(u, v);
lab[u] += lab[v];
lab[v] = u;
}
bool cmp(const int& x, const int& y)
{
return d[x] > d[y];
}
void fix(int l, int r)
{
int i = 0, j = 0;
while(i+j < M)
{
if(dp[l][i].fi+1 > dp[r][j].fi)
{
if(dp[l][i].fi == -1)break;
cur[i+j].fi = dp[l][i].fi+1;
cur[i+j].se = dp[l][i].se;
++i;
}
else
{
cur[i+j] = dp[r][j];
++j;
}
}
for(int j = 0; j < M; j ++)
{
dp[r][j] = cur[j];
cur[j].fi = -1;
}
}
void sol()
{
cin >> n >> m >> q;
for(int i = 1; i <= m; i ++)
{
int x, y;
cin >> x >> y;
adj[y].pb(x);
g[x].pb(y);
}
for(int j = 0; j < M; j ++)cur[j].fi = -1;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j < M; j ++)dp[i][j].fi = -1;
dp[i][0].se = 0;
for(int v: adj[i])fix(v, i);
}
for(int u = 1; u <= n; u ++)
{
vis[u] = true;
vi.pb(u);
for(int v: adj[u])
{
for(int j = 0; j < M; j ++)
{
if(dp[v][j].fi == -1)break;
d[dp[v][j].se] = max(d[dp[v][j].se], dp[v][j].fi+1);
if(!vis[dp[v][j].se])
{
vis[dp[v][j].se] = true;
vi.pb(dp[v][j].se);
}
}
}
sort(vi.begin(), vi.end(), cmp);
for(int j = 0; j < min((int)vi.size(), M); j ++)
{
dp[u][j].fi = d[vi[j]];
dp[u][j].se = vi[j];
}
for(int j = 0; j < (int)vi.size(); j ++)
{
vis[vi[j]] = false;
d[vi[j]] = 0;
}
}
while(q -- > 0)
{
cin >> k >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
vis[a[i]] = true;
}
if(dp[k][0].fi == -1)cout << -1 << '\n';
else
{
ans = -1;
if(n >= M)
{
for(int u = 1; u <= k; u ++)
{
if(vis[u])d[u] = -1;
else d[u] = 0;
}
for(int u = 1; u <= k; u ++)
{
if(d[u] == -1)continue;
for(int v: g[u])d[v] = max(d[v], d[u]+1);
}
ans = d[k];
}
else
{
for(int i = 0; i < M; i ++)
{
if(!vis[dp[k][i].se])
{
ans = dp[k][i].fi;
break;
}
}
}
cout << ans << '\n';
}
for(int i = 1; i <= n; i ++)vis[a[i]] = false;
}
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
Compilation message (stderr)
bitaro.cpp: In function 'int main()':
bitaro.cpp:177:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
177 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:178:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
178 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |