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 sz(v) (int)v.size()
const int MAX = 1e5 + 5;
int SQRT = 350;
int n, m, q, dp[MAX], f[MAX];
vector<int> adj[MAX], rev[MAX];
bool bad[MAX], vis[MAX];
//only take B nodes
vector<int> S[MAX], tmpS;
bool better(int u, int v){
return (dp[u] == dp[v] ? u < v : dp[u] > dp[v]);
}
void addTmpS(int u){
if(!tmpS.empty() && tmpS.back() == u); else tmpS.push_back(u);
}
void insert(vector<int>& A, vector<int>& B){ //define A is larger set
vector<int>().swap(tmpS);
int i = 0, j = 0;
while(i < sz(A) || j < sz(B)){
if(sz(tmpS) == SQRT + 50) break;
if(i == sz(A)) addTmpS(B[j++]);
else if(j == sz(B)) addTmpS(A[i++]);
else if(better(A[i], B[j])) addTmpS(A[i++]);
else addTmpS(B[j++]);
}
swap(tmpS, A);
}
int dfsDP(int u){
if(vis[u]) return dp[u];
vis[u] = true;
for(int v : adj[u]){
dp[u] = max(dp[u], dfsDP(v) + 1);
}
return dp[u];
}
queue<int> qu;
void bfsAdj(int s){
vis[s] = true;
qu.push(s);
while(!qu.empty()){
int u = qu.front(); qu.pop();
for(int v : adj[u]){
insert(S[v], S[u]);
if(!vis[v]){
vis[v] = true;
qu.push(v);
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
for(int i = 0; i < m; ++i){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
rev[v].push_back(u);
}
for(int i = n; i >= 1; --i){
if(!vis[i]){
dfsDP(i);
}
}
SQRT = sqrt(n);
for(int i = 1; i <= n; ++i){ S[i].push_back(i); }
fill(vis + 1, vis + 1 + n, false);
for(int i = 1; i <= n; ++i){
if(!vis[i]){
bfsAdj(i);
}
}
// for(int i = 1; i <= n; ++i){
// cout << i << " : ";
// for(int j : bestNodes[i]) cout << j << ' '; cout << '\n';
// }
while(q--){
int t, y;
cin >> t >> y;
vector<int> nodes(y);
for(int i = 0; i < y; ++i) cin >> nodes[i];
for(int u : nodes) bad[u] = true;
if(y > SQRT){
// cout << "heavy\n";
fill(f + 1, f + 1 + n, -1e9);
f[t] = 0;
for(int i = t; i > 0; --i){
for(int j : rev[i]){
f[j] = max(f[j], f[i] + 1);
}
}
int ans = -1;
for(int i = t; i > 0; --i){
if(!bad[i]) ans = max(ans, f[i]);
}
cout << ans << '\n';
} else{
// cout << "light\n";
int ans = -1;
for(int u : S[t]){
if(!bad[u]){
ans = dp[u];
break;
}
}
if(ans == -1){
cout << ans << '\n';
} else{
ans -= dp[t];
cout << ans << '\n';
}
}
for(int u : nodes) bad[u] = false;
}
return 0;
}
/*
test 1 :
5 6 3
1 2
2 4
3 4
1 3
3 5
4 5
4 1 1
5 2 2 3
2 3 1 4 5
test 2 :
12 17 10
1 2
2 3
3 4
1 5
2 6
3 7
4 8
5 6
6 7
7 8
5 9
6 10
7 11
8 12
9 10
10 11
11 12
6 3 1 7 12
3 7 1 2 3 4 5 6 7
11 3 1 3 5
9 2 1 9
8 4 1 2 3 4
1 1 1
12 0
10 3 1 6 10
11 8 2 3 5 6 7 9 10 11
8 7 2 3 4 5 6 7 8
test 3 :
12 17 1
1 2
2 3
3 4
1 5
2 6
3 7
4 8
5 6
6 7
7 8
5 9
6 10
7 11
8 12
9 10
10 11
11 12
12 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... |