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>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<double, double> point;
typedef tuple<int, int, int> tp; //-------LL
typedef unordered_set<ll> uset;
typedef priority_queue<pll, vector<pll>, greater<pll>> djk;
typedef long long C;
typedef complex<C> P;
#define pb push_back
#define ms(a,b) memset(a,b,sizeof(a))
#define maxE(a) *max_element(a, a+sizeof(a)/sizeof(a[0]))
#define minE(a) *min_element(a, a+sizeof(a)/sizeof(a[0]))
#define F first
#define S second
#define siz(a) (int)a.size()
#define len(a) (int)a.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bits(x) __builtin_popcount(x)
#define vvll vector<vector<ll>>
#define FOR(a, n) for (int a = 0; a < n; a++)
#define fin(s) {cout << s << "\n"; return;}
#define finm(s) {cout << s << "\n"; return 0;}
#define X real()
#define Y imag()
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cout << vars << " = ";
string delim = "";
(..., (cout << delim << values, delim = ", "));
cout << endl;
}
template<class T>
istream& operator >> (istream& is, complex<T>& p) {
T value;
is >> value; p.real(value);
is >> value; p.imag(value);
return is;
}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 1e9+7;
const double pie = 2*acos(0.0);
//------------------------------------------------------------
const int MAX = 1e5+5;
const int BLOCK = 80;
vector<int> adj[MAX];
vector<pi> best[MAX];
bool bad[MAX];
int dp[MAX];
int n, m, q;
bool comp(pi a, pi b){
if (a.S != b.S) return a.S < b.S;
return a.F > b.F;
}
bool rem(pi a, pi b){
return a.S == b.S;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> q;
for (int i = 1; i <= m; i++){
int a, b; cin >> a >> b;
adj[a].pb(b);
}
for (int v = 1; v <= n; v++){
best[v].pb({0, v});
sort(all(best[v]), comp);
best[v].resize(distance(best[v].begin(), unique(all(best[v]), rem)));
sort(rall(best[v]));
while (siz(best[v]) > BLOCK) best[v].pop_back();
for (int u: adj[v]){
for (auto [dis, src]: best[v]) best[u].pb({dis+1, src});
}
}
while (q--){
int v, tot; cin >> v >> tot;
if (tot < BLOCK){
unordered_set<int> st;
for (int i = 1; i <= tot; i++){
int a; cin >> a; st.insert(a);
}
bool done = false;
for (auto [dis, src]: best[v]){
if (!st.count(src)){
cout << dis << "\n";
done = true; break;
}
}
if (!done){
assert(siz(best[v]) < BLOCK);
cout << -1 << "\n";
}
} else {
ms(bad, false); ms(dp, -1);
for (int i = 1; i <= tot; i++){
int a; cin >> a; bad[a] = true;
}
for (int i = 1; i <= v; i++){
if (!bad[i]) dp[i] = max(0, dp[i]);
if (dp[i] != -1) for (int u: adj[i]) dp[u] = max(dp[u], dp[i]+1);
}
cout << dp[v] << "\n";
}
}
}
/* stuff you should look for
* int overflow, array bounds
* test case inputs don't carry over (DON'T RETURN EARLY)
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |