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;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
const int N = 1e5 + 3;
const int B = 320;
int n, m, q;
vector<int> g[N], rg[N];
vector<pii> best_paths[N];
bool vis[N];
int c[N];
bool busy[N];
int f[N];
void prep(){ // B largest paths to each node
for(int i = 1; i <= n; ++i){
best_paths[i].push_back({i, 0});
for(int p : rg[i]){
// avoid repeated sources
// merge sort
vector<pii> yoin;
int x = 0, y = 0;
while(x < sz(best_paths[p]) or y < sz(best_paths[i])){
if(sz(yoin) == B)
break;
while(x < sz(best_paths[p]) and vis[best_paths[p][x].F])
++x;
while(y < sz(best_paths[i]) and vis[best_paths[i][y].F])
++y;
pii u = (x < sz(best_paths[p]) ? best_paths[p][x] : make_pair(-1, -1));
pii v = (y < sz(best_paths[i]) ? best_paths[i][y] : make_pair(-1, -1));
if(u.F != -1 and u.S + 1 > v.S){
u.S += 1;
yoin.push_back(u);
vis[u.F] = 1;
++x;
}
else if(v.F != -1){
yoin.push_back(v);
vis[v.F] = 1;
++y;
}
}
best_paths[i] = yoin;
for(int j = 0; j < sz(yoin); ++j)
vis[yoin[j].F] = 0;
}
debug(i, best_paths[i]);
}
}
void solve(){
cin >> n >> m >> q;
for(int i = 1; i <= m; ++i){
int u, v;
cin >> u >> v;
rg[v].push_back(u);
}
prep();
while(q--){
int t, y;
cin >> t >> y;
for(int i = 1; i <= y; ++i){
cin >> c[i];
busy[c[i]] = 1;
}
if(y < B){
int res = -1;
for(auto [v, x] : best_paths[t])
if(!busy[v])
res = max(res, x);
if(!busy[t])
res = max(res, 0);
cout << res << '\n';
}
else{
memset(f, -1, sizeof(f));
for(int i = 1; i <= t; ++i){
if(!busy[i])
f[i] = 0;
for(int v : rg[i])
if(f[v] != -1)
f[i] = max(f[i], f[v] + 1);
}
cout << f[t] << '\n';
}
for(int i = 1; i <= y; ++i)
busy[c[i]] = 0;
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
Compilation message (stderr)
bitaro.cpp: In function 'int32_t main()':
bitaro.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | 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... |