This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author : Lăng Trọng Đạt
* created: 12-07-2024
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
using vi = vector<int>;
#define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
void mx(int& a, int b) { if (b > a) a = b; }
void mi(int& a, int b) { if (b < a) a = b; }
#define si(x) (int)(x.size())
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int B = 100;
vector<int> adj[MAXN];
int g[MAXN], in[MAXN];
int n, m, q, a, b;
vector<pii> len[MAXN]; // {end node, longest length}
bool block[MAXN];
int dp[MAXN];
void dfs(int v, int type) {
db(v, type, dp[v])
dp[v] = -2;
for (int u : adj[v]) {
if (type == 1) {
if (dp[u] == -1)
dfs(u, type);
if (dp[u] != -2) mx(dp[v], dp[u] + 1);
} else if (dp[u] != -1) {
dfs(u, type);
}
}
if (!block[v]) mx(dp[v], 0);
if (type == 0) dp[v] = -1;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("hi.inp", "r")) {
// freopen("hi.inp", "r", stdin);
freopen("./bitaro-data/bitaro/in/03-81.txt", "r", stdin);
freopen("hi.out", "w", stdout);
}
cin >> n >> m >> q;
FOR(i, 1, m) {
cin >> a >> b;
adj[b].push_back(a);
in[a]++;
}
vector<int> node;
FOR(i, 1, n) if (in[i] == 0) node.push_back(i);
vector<int> order;
while (si(node)) {
int v = node.back(); node.pop_back();
order.push_back(v);
for (int u : adj[v]) {
if (--in[u] == 0) {
node.push_back(u);
}
}
}
reverse(all(order));
vector<int> pos(n + 1, -1);
for (int v : order) {
len[v].push_back({v, 0});
for (int u : adj[v]) {
for (auto[a, b] : len[u]) {
if (pos[a] != -1) mx(len[v][pos[a]].se, b + 1);
else len[v].push_back({a, b + 1});
}
sort(all(len[v]), [](pii& a, pii& b) -> bool {return a.se > b.se;});
while (si(len[v]) > B + 5) {pos[len[v].back().f] = -1; len[v].pop_back(); }
FOR(id, 0, si(len[v]) - 1) {
pos[len[v][id].f] = id;
}
}
FOR(id, 0, si(len[v]) - 1) {
pos[len[v][id].f] = -1;
}
}
// db("fuck")
// FOR(i, 1, n) {
// db(i, len[i]);
// }
memset(dp, -1, sizeof(dp));
FOR(i, 1, q) {
int source, t;
cin >> source >> t;
// db(source, t)
vector<int> c(t);
for (int& v : c) {
cin >> v;
block[v] = 1;
}
if (i == 61)
db(source, t)
int ans = -1;
if (t > B) {
// dfs(source, 1);
// ans = dp[source];
// dfs(source, 0);
for (int v : order) {
for (int u : adj[v]) {
if (dp[u] != -1)
mx(dp[v], dp[u] + 1);
}
if (!block[v]) mx(dp[v], 0);
if (v == source) {
ans = dp[v];
break;
}
}
for (int v : order) {
dp[v] = -1;
if (v == source) break;
}
} else {
for (auto[u, l] : len[source]) {
if (!block[u]) {
ans = l;
break;
}
}
}
cout << (ans < 0 ? -1 : ans) << "\n";
for (int v : c) {
block[v] = 0;
}
}
}
Compilation message (stderr)
bitaro.cpp: In function 'int32_t main()':
bitaro.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
| ^
bitaro.cpp:60:5: note: in expansion of macro 'FOR'
60 | FOR(i, 1, m) {
| ^~~
bitaro.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
| ^
bitaro.cpp:67:5: note: in expansion of macro 'FOR'
67 | FOR(i, 1, n) if (in[i] == 0) node.push_back(i);
| ^~~
bitaro.cpp:84:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
84 | for (auto[a, b] : len[u]) {
| ^
bitaro.cpp:18:31: warning: unnecessary parentheses in declaration of 'id' [-Wparentheses]
18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
| ^
bitaro.cpp:90:13: note: in expansion of macro 'FOR'
90 | FOR(id, 0, si(len[v]) - 1) {
| ^~~
bitaro.cpp:18:31: warning: unnecessary parentheses in declaration of 'id' [-Wparentheses]
18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
| ^
bitaro.cpp:94:9: note: in expansion of macro 'FOR'
94 | FOR(id, 0, si(len[v]) - 1) {
| ^~~
bitaro.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
| ^
bitaro.cpp:103:5: note: in expansion of macro 'FOR'
103 | FOR(i, 1, q) {
| ^~~
bitaro.cpp:135:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
135 | for (auto[u, l] : len[source]) {
| ^
bitaro.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | freopen("./bitaro-data/bitaro/in/03-81.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:56:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | freopen("hi.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |