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 idata vector<int>
#define igrid vector<idata>
#define di pair<int, int>
int nbNodes, nbRoads, nbQuery;
igrid roads;
const int BLOCK_SIZE = 400;
struct VBO {
unordered_map<int, int> sdd_reverse;
set<di, greater<>> sdd;
void insert (int node, int depth) {
if (sdd_reverse.find(node) != sdd_reverse.end()) {
int max_depth = (*sdd_reverse.find(node)).second;
if (max_depth >= depth) return ;
sdd_reverse.erase(node);
sdd.erase(di(max_depth, node));
}
sdd_reverse.insert(di(node, depth));
sdd.insert(di(depth, node));
if (sdd.size() <= BLOCK_SIZE) return ;
auto it = sdd.end();
it --;
int rnode = (*it).second;
int rdepth = (*it).first;
sdd_reverse.erase(rnode);
sdd.erase(di(rdepth, rnode));
}
int query (set<int> &disabled) {
for (di data : sdd) {
if (disabled.find(data.second) != disabled.end()) continue ;
return data.first;
}
return -1;
}
void merge (VBO &other) {
for (di data : other.sdd)
this->insert(data.second, data.first + 1);
}
};
vector<VBO> vbos;
vector<int> bfs_v;
vector<int> dfs_v;
vector<int> visited;
int stage = 1;
void dfs (int node) {
vbos[node].insert(node, 0);
for (int prev : roads[node]) {
if (vbos[node].sdd.size() == 0)
dfs(prev);
vbos[node].merge(vbos[prev]);
}
}
void _c_dfs (int node) {
if (visited[node] == stage) return ;
visited[node] = stage;
for (int prev : roads[node]) {
dfs_v[prev] ++;
_c_dfs(prev);
}
}
int _compute (int start, set<int> &disabled) {
queue<int> nodes;
nodes.push(start);
fill(dfs_v.begin(), dfs_v.end(), 0);
_c_dfs(start);
stage ++;
fill(bfs_v.begin(), bfs_v.end(), -1);
bfs_v[start] = 0;
int answer = -1;
while (nodes.size() != 0) {
int curr = nodes.front();
nodes.pop();
if (disabled.find(curr) == disabled.end())
answer = max(answer, bfs_v[curr]);
for (int prev : roads[curr]) {
dfs_v[prev] --;
if (dfs_v[prev] != 0) continue ;
bfs_v[prev] = bfs_v[curr] + 1;
nodes.push(prev);
}
}
return answer;
}
int compute (int node, set<int> &disabled) {
if (disabled.size() <= BLOCK_SIZE)
return vbos[node].query(disabled);
return _compute(node, disabled);
}
int main () {
cin >> nbNodes >> nbRoads >> nbQuery;
vbos .resize(nbNodes);
roads.resize(nbNodes);
visited.resize(nbNodes);
dfs_v.resize(nbNodes);
bfs_v.resize(nbNodes);
for (int idRoad = 0; idRoad < nbRoads; idRoad ++) {
int a, b;
cin >> a >> b;
a --; b --;
roads[b].push_back(a);
}
for (int idNode = 0; idNode < nbNodes; idNode ++) {
if (vbos[idNode].sdd.size() != 0) continue ;
dfs(idNode);
}
for (int idQuery = 0; idQuery < nbQuery; idQuery ++) {
int target, dCount;
cin >> target >> dCount;
target --;
set<int> disabled;
for (int idD = 0; idD < dCount; idD ++) {
int vd;
cin >> vd;
vd --;
disabled.insert(vd);
}
cout << compute(target, disabled) << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |