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;
void fastIO(){
ios_base::sync_with_stdio(false);
cin.tie(0);
}
const int SQRT = 316;
vector<int> arr[100100];
vector<pair<int, int> > far[100100];
int dist[100100];
int vis[100100];
bool cmp(int a, int b){
return dist[a] > dist[b];
}
int main(){
fastIO();
int N, M, Q;
cin >> N >> M >> Q;
for (int i = 1; i <= M; i++){
int a, b; cin >> a >> b;
arr[b].push_back(a);
}
for (int i = 1; i <= N; i++){
vector<int> all;
all.push_back(i);
dist[i] = 0;
for (int j = 0; j < (int)arr[i].size(); j++){
int nx = arr[i][j];
for (int k = 0; k < (int)far[nx].size(); k++){
if (vis[far[nx][k].second] != i){
dist[far[nx][k].second] = far[nx][k].first+1;
all.push_back(far[nx][k].second);
vis[far[nx][k].second] = i;
} else {
dist[far[nx][k].second] = max(dist[far[nx][k].second], far[nx][k].first+1);
}
}
}
sort(all.begin(), all.end(), cmp);
for (int j = 0; j < min((int)all.size(), SQRT); j++){
far[i].push_back({dist[all[j]], all[j]});
}
}
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= Q; i++){
int T, R; cin >> T >> R;
for (int j = 1; j <= R; j++){
int y; cin >> y;
vis[y] = i;
}
if (R < SQRT){
for (int j = 0; j < (int)far[T].size(); j++){
int nx = far[T][j].second;
if (vis[nx] != i){
cout << far[T][j].first << "\n";
break;
}
}
} else {
memset(dist, -1, sizeof(dist));
for (int j = 1; j <= T; j++){
if (vis[j] != i){
dist[j] = max(dist[j], 0);
}
for (int nx : arr[j]){
dist[nx] = max(dist[nx], dist[j]+1);
}
}
cout << dist[T] << "\n";
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |