This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e5+5, sq = 500;
int n, m, q, dist[maxn];
bool mark[maxn];
pii dp[maxn][sq];
vector <int> adj[maxn], p[maxn];
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int v, u; cin >> v >> u;
adj[--u].pb(--v);
p[u].pb(0);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < sq; j++) dp[i][j] = mp(-1, -1);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < sq; j++) {
int mx = -1, v = -1, googo = -1;
for (int k = 0; k < adj[i].size(); k++) {
if (p[i][k] < sq) {
if (dp[adj[i][k]][p[i][k]].F+1 > mx and dp[adj[i][k]][p[i][k]].F != -1) {
if (!mark[dp[adj[i][k]][p[i][k]].S]) {
mx = dp[adj[i][k]][p[i][k]].F+1, v = dp[adj[i][k]][p[i][k]].S;
googo = k;
}
}
}
}
dp[i][j] = mp(mx, v);
if (v != -1) mark[v] = 1;
if (googo != -1) p[i][googo]++;
}
for (int j = 0; j < sq; j++) {
if (dp[i][j].F == -1) {
dp[i][j].F = 0, dp[i][j].S = i;
break;
}
mark[dp[i][j].S] = 0;
}
}
memset(mark, 0, sizeof mark);
while (q--) {
vector <int> vec;
int v, c; cin >> v >> c; v--;
for (int i = 0; i < c; i++) {
int u; cin >> u; vec.pb(--u); mark[u] = 1;
}
if (c <= sq-5) {
int ans = -1;
for (int i = 0; i < sq; i++) {
if (dp[v][i].F == -1) break;
if (!mark[dp[v][i].S]) {
ans = dp[v][i].F; break;
}
}
cout << ans << '\n';
}
else {
memset(dist, 0, sizeof dist);
for (int i = 0; i < n; i++) {
if (mark[i]) dist[i] = inf;
for (int u : adj[i]) smax(dist[i], dist[u]+1);
}
if (dist[v] >= 0) cout << dist[v] << '\n';
else cout << -1 << '\n';
}
for (int u : vec) mark[u] = 0;
}
return 0;
}
Compilation message (stderr)
bitaro.cpp: In function 'int32_t main()':
bitaro.cpp:38:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int k = 0; k < adj[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |