This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// no matter how hard you work, someone else is working harder
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _i = (b); i <= _i; ++ i)
#define FORD(i, b, a) for (int i = (b), _i = (a); i >= _i; -- i)
#define REP(i, b) for (int i = 0, _i = (b); i < _i; ++ i)
#define FORE(i, a) for (auto& i : a)
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define cntbit __builtin_popcountll
#define len(x) (int)x.size()
#define bit(x, i) (((x) >> (i)) & 1)
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
template <typename A, typename B>
inline bool ckmax(A& a, const B& b) {
if (a < b) {
a = b;
return true;
} return false;
}
template <typename A, typename B>
inline bool ckmin(A& a, const B& b) {
if (a > b) {
a = b;
return true;
} return false;
}
inline void fileIO(const string& Task = "") {
ios::sync_with_stdio(false); cin.tie(NULL);
if (fopen((Task + ".inp").c_str(), "r")) {
freopen((Task + ".inp").c_str(), "r", stdin);
freopen((Task + ".out").c_str(), "w", stdout);
}
}
/* Author: Hoang Quoc Viet */
/** END OF TEMPLATE **/
const int B = 350;
int n, m, q;
vector<int> revAdj[100005];
vector<pair<int, int>> far_from[100005];
int timer = 0, used[100005];
int main() {
fileIO("main");
cin >> n >> m >> q;
FOR(i, 1, m) {
int u, v;
cin >> u >> v;
revAdj[v].pb(u);
}
FOR(u, 1, n) {
far_from[u].pb(mp(0, u));
FORE(v, revAdj[u]) {
vector<pair<int, int>> tmp = far_from[v], cur = far_from[u], res;
FORE(cc, tmp) ++ cc.F;
int run_tmp = 0, run_cur = 0;
++ timer;
while (run_tmp < len(tmp) && run_cur < len(cur) && len(res) < B) {
if (tmp[run_tmp].F >= cur[run_cur].F) {
if (used[tmp[run_tmp].S] != timer) {
used[tmp[run_tmp].S] = timer;
res.pb(tmp[run_tmp]);
}
++ run_tmp;
} else {
if (used[cur[run_cur].S] != timer) {
used[cur[run_cur].S] = timer;
res.pb(cur[run_cur]);
}
++ run_cur;
}
}
while (run_tmp < len(tmp) && len(res) < B) {
if (used[tmp[run_tmp].S] != timer) {
used[tmp[run_tmp].S] = timer;
res.pb(tmp[run_tmp]);
}
++ run_tmp;
}
while (run_cur < len(cur) && len(res) < B) {
if (used[cur[run_cur].S] != timer) {
used[cur[run_cur].S] = timer;
res.pb(cur[run_cur]);
}
++ run_cur;
}
far_from[u] = res;
}
}
while (q --) {
int u, y;
cin >> u >> y;
++ timer;
FOR(i, 1, y) {
int x;
cin >> x;
used[x] = timer;
}
if (y >= B) {
vector<int> dp(n+5, -1);
for (int i = 1; i <= u; ++ i) {
if (used[i] != timer) dp[i] = 0;
FORE(j, revAdj[i]) if (dp[j] != -1) {
ckmax(dp[i], dp[j] + 1);
}
}
cout << dp[u] << "\n";
} else {
int res = -1;
FORE(p, far_from[u]) if (used[p.S] != timer) {
ckmax(res, p.F);
}
cout << res << "\n";
}
}
return 0;
}
/*** VOI VOI VOI important things must be said three times :> ***/
Compilation message (stderr)
bitaro.cpp: In function 'void fileIO(const string&)':
bitaro.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((Task + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((Task + ".out").c_str(), "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... |