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>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
const int b = 1010;
set<pair<int,int> > st[maxn];
vector<int>g[maxn], ig[maxn];
vector<int>order;
bool visited[maxn];
int n, m, q;
void dfs(int node) {
if(visited[node]) return;
visited[node] = true;
for(int i:g[node]) {
if(!visited[i]) {
dfs(i);
}
}
st[node].insert(mp(0, node));
order.pb(node);
}
void preprocess() {
for(int i=1;i<=n;i++) {
if(!visited[i]) dfs(i);
}
reverse(order.begin(), order.end());
for(int node:order) {
map<int, int> dists;
for(int i:ig[node]) {
//update st[node] with values from st[i] + 1
// Possible optimization point, in case of TLE
for(auto vals:st[i]) {
dists[vals.second] = max(dists[vals.second], vals.first + 1);
}
}
for(auto i:dists) {
st[node].insert(mp(i.second, i.first));
if(st[node].size() > b) st[node].erase(st[node].begin());
}
}
}
bool blocked[maxn];
void solve() {
int node, qs, x;
vector<int>points;
cin>>node>>qs;
for(int i=0;i<qs;i++) {
cin>>x;
points.pb(x);
blocked[x] = true;
}
if(qs > b) return; // DO DFS HERE
int maxtunnels = -1;
for(auto i:st[node]) {
if(!blocked[i.second]) maxtunnels = max(maxtunnels, i.first);
}
cout<<maxtunnels<<"\n";
for(int i:points) blocked[i] = false;
}
int main() {
cin>>n>>m>>q;
int a, b;
for(int i=0;i<m;i++) {
cin>>a>>b;
g[a].pb(b);
ig[b].pb(a);
}
preprocess();
while(q--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |