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<pair<int, int>> dis[2 * 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);
}
for (int i = 1;i <= n;i++) {
vector<pair<int, int>> t;
t.push_back({i, 0});
for (auto u : in[i]) {
t.push_back({u, 1});
for (auto x : dis[u])
t.push_back({x.f, x.s + 1});
}
sort(t.begin(), t.end(), [&](pair<int, int> x, pair<int, int> y) {
return x.s > y.s;
});
for (int j = 0;j < min(B + 10, (int)t.size());j++)
dis[i].push_back(t[j]);
}
while (q--) {
int s, c;
cin >> s >> c;
int a[c];
for (int i = 0;i < c;i++) {
cin >> a[i];
cnt[a[i]] = 1;
}
int ans = -cnt[s];
if (c < B) {
for (auto u : dis[s]) {
if (!cnt[u.f])
ans = max(ans, u.s);
}
}
else {
for (int i = 1;i <= n;i++) {
d[i] = -1;
cnt[i] = 0;
}
d[s] = 0;
for (int i = s;i >= 1;i--) {
if (d[i] >= 0) {
if (!cnt[i])
ans = max(ans, d[i]);
for (auto u : in[i]) {
d[u] = max(d[u], d[i] + 1);
}
}
}
}
cout << ans << '\n';
for (int i = 0;i < c;i++)
cnt[a[i]] = 0;
}
}
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... |