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 target("avx2")
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T>
bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
};
template <typename T>
bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
};
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
for(int i = 0; i < v.size(); i++) {
os << v[i] << (i + 1 == v.size() ? "\n" : " ");
}
return os;
}
template <typename T>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
for(auto& e : t) {
fill_v(e, v);
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> G(n);
int sq = 320;
vector<vector<int>> dp(n, vector<int>(sq + 1, -INF)),
idx(n, vector<int>(sq + 1, -1));
vector<int> sz(n), vis(n);
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
--a;
--b;
G[a].push_back(b);
}
for(int i = 0; i < n; i++) {
dp[i][0] = 0;
idx[i][0] = i;
sz[i] = 1;
}
vector<int> ndp(sq + 1, -INF), nid(sq + 1, -1);
for(int i = 0; i < n; i++) {
for(auto& e : G[i]) {
int a = 0, b = 0, t = 0;
while(a < sz[i] || b < sz[e]) {
if(t > sq) {
break;
}
while(a < sz[i] && vis[idx[i][a]]) {
a++;
}
while(b < sz[e] && vis[idx[e][b]]) {
b++;
}
if(a >= sz[i] && b >= sz[e]) {
break;
}
if(a >= sz[i]) {
ndp[t] = dp[e][b];
nid[t] = idx[e][b];
b++;
} else if(b >= sz[e] || dp[i][a] + 1 > dp[e][b]) {
ndp[t] = dp[i][a] + 1;
nid[t] = idx[i][a];
a++;
} else {
ndp[t] = dp[e][b];
nid[t] = idx[e][b];
b++;
}
vis[nid[t]] = 1;
t++;
}
sz[e] = t;
for(int j = 0; j < t; j++) {
dp[e][j] = ndp[j];
idx[e][j] = nid[j];
vis[nid[j]] = 0;
}
}
}
vector<int> dp2(n, -INF), c(100010);
while(q--) {
int t, y;
cin >> t >> y;
--t;
for(int i = 0; i < y; i++) {
cin >> c[i];
--c[i];
vis[c[i]] = 1;
}
int ans = -INF;
if(y <= sq) {
for(int i = 0; i <= sq; i++) {
if(!vis[idx[t][i]]) {
ans = dp[t][i];
break;
}
}
} else {
for(int i = 0; i < n; i++) {
if(!vis[i]) {
chmax(dp2[i], 0);
}
if(i == t) {
ans = dp2[t];
}
for(auto& e : G[i]) {
chmax(dp2[e], dp2[i] + 1);
}
dp2[i] = -INF;
}
}
cout << max(ans, -1) << "\n";
for(int i = 0; i < y; i++) {
vis[c[i]] = 0;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |