이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define debug(x) {cout << #x << ": "; for (auto it : x) cout << it << " ";cout << endl;}
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000007
#define LOGN 15
#define MAXN 100005
const int SQRT = 1500;
vector<vector<int>> graph, rev;
vector<vector<pair<int,int>>> dist;
bool closed[MAXN];
void get(int to, int from) {
vector<pair<int,int>> new_l;
int l = 0;
int sz = dist[to].size();
for (auto u : dist[from]) {
while (l < sz && dist[to][l].f < u.f) {
new_l.push_back(dist[to][l]);
l++;
}
new_l.push_back({u.f, u.s+1});
if (l < sz && dist[to][l].f == new_l.back().f && dist[to][l].s > new_l.back().s) {
new_l.pop_back();
new_l.push_back({u.f, dist[to][l].s});
l++;
}
if (l < sz && dist[to][l].f == new_l.back().f)
l++;
}
sort(new_l.begin(), new_l.end(), [&](pair<int,int> &a, pair<int,int> &b) {
return a.s > b.s;
});
while (new_l.size() > SQRT)
new_l.pop_back();
sort(new_l.begin(), new_l.end());
dist[to] = new_l;
}
int res = -1;
void dfs(int node, int parent, int dist) {
if (!closed[node])
res = max(res, dist);
for (auto u : rev[node])
dfs(u, node, dist + 1);
}
int main() {
fast
int N, M, Q, S, E;
cin >> N >> M >> Q;
graph = vector<vector<int>>(N+1, vector<int>());
rev = vector<vector<int>>(N+1, vector<int>());
dist = vector<vector<pair<int,int>>>(N+1, vector<pair<int,int>>());
for (int i = 0; i < M; i++) {
cin >> S >> E;
graph[S].push_back(E);
rev[E].push_back(S);
}
for (int i = 1; i <= N; i++) {
dist[i].push_back({i, 0});
for (auto u : graph[i])
get(u, i);
}
while (Q--) {
int town, cnt;
cin >> town >> cnt;
vector<int> towns(cnt);
for (int i = 0; i < cnt; i++) {
cin >> towns[i];
closed[towns[i]] = true;
}
if (cnt >= SQRT) {
res = -1;
dfs(town, town, 0);
cout << res << "\n";
} else {
int ans = -1;
for (auto u : dist[town]) {
if (!closed[u.f])
ans = max(ans, u.s);
}
cout << ans << "\n";
}
for (auto u : towns)
closed[u] = false;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |