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>
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
using ll = long long;
int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int, int> pairs;
const int INF = 1e9 + 1;
const int N = 2e5 + 100;
const double eps = 1e-7;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <typename XPAX>
void ckma(XPAX &x, XPAX y) {
x = (x < y ? y : x);
}
template <typename XPAX>
void ckmi(XPAX &x, XPAX y) {
x = (x > y ? y : x);
}
const int B = 120;
int n, m, q;
V<int> val(N), y(N, -1), vis(N, -1);
V<pairs> far[N];
V<int> g[N];
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
void solve() {
cin >> n >> m >> q;
forn(i, m) {
int a, b;
cin >> a >> b;
--a;--b;
g[b].push_back(a);
}
auto cmp = [&](int x, int y) {
return val[x] > val[y];
};
forn(i, n) {
V<int> ps;
for(auto x : g[i]) {
for(auto [d, p] : far[x]) {
if(vis[p] != i) {
ps.push_back(p);
vis[p] = i;
val[p] = d + 1;
}
else
ckma(val[p], d + 1);
}
}
ps.push_back(i);
sort(all(ps), cmp);
forn(j, min((int)ps.size(), B)) {
far[i].push_back({val[ps[j]], ps[j]});
}
//debug(far[i]);
}
while(q--) {
int t, Y;
cin >> t >> Y;
--t;
forn(i, Y) {
int ks;
cin >> ks;
y[ks - 1] = q;
}
if(Y >= B) {
V<int> dp(n, -1);
forn(i, n) {
if(y[i] != q)
ckma(dp[i], 0);
for(auto ms : g[i])
if(dp[ms] != -1)ckma(dp[i], dp[ms] + 1);
}
cout << dp[t] << '\n';
}
else {
for(auto x : far[t]) {
if(y[x.second] != q) {
cout << x.first<< '\n';
goto DONE;
}
}
cout << -1 << '\n';
DONE: continue;
}
}
}
void test_case() {
int t;
cin >> t;
forn(p, t) {
//cout << "Case #" << p + 1 << ": ";
solve();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |