This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
// Hope is last to die
// Khodaya khodet komak kon
// Let's go
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
const int maxn = 1e5 + 10, msq = 320 + 10;
const int inf = 1e9 + 7;
int n, m, q, sq, cur[maxn], val[maxn];
pii dp[maxn][msq];
vector<int> adj[maxn], party;
bool mark[maxn];
void calcdp(){
for(int i = 0; i < n; i++){
for(int j = 0; j < sq; j++) dp[i][j] = mp(-1, 0);
}
for(int i = 1; i <= n; i++){
for(int j = 0; j < sq; j++){
pii res = mp(-1, 0);
for(int k = 0; k < sz(adj[i]); k++){
int u = adj[i][k];
while(mark[dp[u][cur[u]].S]) cur[u]++;
if(!dp[u][cur[u]].S) continue;
if(dp[u][cur[u]].F + 1 > res.F) res = mp(dp[u][cur[u]].F + 1, dp[u][cur[u]].S);
}
dp[i][j] = res;
if(res.S) mark[res.S] = 1;
}
for(int j = 0; j < sq; j++){
mark[dp[i][j].S] = 0;
if(dp[i][j].F == -1){
dp[i][j] = mp(0, i);
break;
}
}
for(auto u: adj[i]) cur[u] = 0;
}
}
int get(int v){
for(int i = 1; i <= v; i++){
val[i] = (mark[i] ? -1 : 0);
for(auto u: adj[i]){
if(~val[u]) smax(val[i], val[u] + 1);
}
}
return val[v];
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
sq = 200;
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v;
adj[v].pb(u);
}
calcdp();
while(q--){
int v, y; cin >> v >> y;
party.clear();
for(int i = 0; i < y; i++){
int x; cin >> x;
mark[x] = 1;
party.pb(x);
}
if(y >= sq) cout << get(v) << '\n';
else{
for(int i = 0; i < sq; i++){
if(!mark[dp[v][i].S]){
cout << dp[v][i].F << '\n';
break;
}
}
}
for(auto u: party) mark[u] = 0;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |