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 ll int
#define pll pair<int, int>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 3e5 + 100;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const ll block = 330;
ll n,m,q;
vector<ll>adj[N];
pll dp[N][block + 3];
ll f[N];
bool good[N];
struct ccjv{ll val, u, idx;};
struct cmp{
bool operator()(const ccjv &a, const ccjv &b){
return a.val < b.val;
}
};
void to_thic_cau(){
cin >> n >> m >> q;
for(int i = 1; i <= m;i++){
ll u,v; cin >> u >> v;
adj[v].pb(u);
}
for(int i = 1; i <= n;i++){
for(int j = 1; j <= block;j++) dp[i][j] = {-inf, 0};
}
for(int i = 1; i <= n;i++){
priority_queue<ccjv, vector<ccjv>, cmp>q;
q.push({-1, i, 1});
for(auto j : adj[i]) q.push({dp[j][1].F, j, 1});
for(int j = 1; j <= block;j++){
if(!q.size()) break;
ll u = q.top().u, c = q.top().val, idx = q.top().idx; q.pop();
dp[i][j] = {c + 1, (u == i ? i : dp[u][idx].S)};
if(u != i) q.push({dp[u][idx + 1].F, u, idx + 1});
}
}
good[0] = 1;
while(q--){
ll t, sz; cin >> t >> sz;
vector<ll>vec;
for(int i = 1; i <= sz;i++){
ll x; cin >> x;
vec.pb(x);
good[x] = 1; f[x] = -inf;
}
if(t >= block){
for(int i = 1; i <= n;i++) for(auto j : adj[i]) f[i] = max(f[i], f[j] + 1);
cout << (f[t] < 0 ? -1 : f[t]) << '\n';
for(auto x : vec) good[x] = 0, f[x] = 0;
}
else{
bool ok = 0;
for(int i = 1; i <= block;i++){
if(!good[dp[t][i].S]){
cout << dp[t][i].F << '\n';
ok = 1;
break;
}
}
if(!ok) cout << -1 << "\n";
for(auto x : vec) good[x] = 0;
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |