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 f first
#define s second
#define int long long
const int N = 1e5 + 69;
const int B = 320;
int n, m, q, deg[N], cnt[N], dp[N], deg1[N], d[N];
vector<int> g[N], in[N];
set<pair<int, int>> bst[N];
vector<int> dis[B];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> m >> q;
for (int i = 1;i <= m;i++) {
int a, b;
cin >> a >> b;
if (a > b)
swap(a, b);
g[a].push_back(b);
in[b].push_back(a);
deg[b]++;
deg1[b]++;
}
queue<int> que;
vector<int> p;
for (int i = 1;i <= n;i++) {
dp[i] = 1e9;
if (deg[i] == 0) {
dp[i] = 0;
que.push(i);
}
}
while (!que.empty()) {
auto v = que.front();
que.pop();
if (p.size() < B)
p.push_back(v);
for (auto u : g[v]) {
if (dp[u] <= dp[v] + 1)
continue;
dp[u] = dp[v] + 1;
que.push(u);
}
}
sort(p.begin(), p.end());
for (int j = 0;j < (int)p.size();j++) {
for (int i = 1;i <= n;i++)
dp[i] = 0, deg[i] = deg1[i];
que.push(p[j]);
while (!que.empty()) {
auto v = que.front();
que.pop();
for (auto u : g[v]) {
--deg[u];
dp[u] = max(dp[u], dp[v] + 1);
if (deg[u] == 0)
que.push(u);
}
}
for (int i = 1;i <= n;i++)
dis[j].push_back(dp[i]);
}
while (q--) {
int s, c;
cin >> s >> c;
int a[c];
for (int i = 0;i < c;i++)
cin >> a[i];
int ans = 0;
if (c < B) {
sort(a, a + c);
for (int i = 0;i < (int)p.size();i++) {
auto u = lower_bound(a, a + c, p[i]) - a;
if (u == c || a[u] != p[i])
ans = max(ans, dis[i][s - 1]);
}
}
else {
for (int i = 1;i <= n;i++)
d[i] = cnt[i] = 0;
for (int i = 1;i <= n;i++)
cnt[a[i]] = 1;
que.push(s);
while (!que.empty()) {
auto v = que.front();
que.pop();
for (auto u : in[v]) {
if (d[u] >= d[v] + 1)
continue;
d[u] = d[v] + 1;
que.push(v);
}
}
for (int i = 1;i <= n;i++) {
if (!cnt[i])
ans = max(ans, d[i]);
}
}
cout << ans << '\n';
}
}
return 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... |