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 <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x).size()
#define derr if(1) cerr
typedef vector<int> vi;
// END NO SAD
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int SAVE = 30;
vector<pii> best[100005];
vector<int> edges[100005];
int excluded[100005];
int n;
int dp[100005];
void solve(int dest, int id) {
memset(dp, -1, sizeof(dp));
for(int i = 1; i <= dest; i++) {
if(excluded[i] != id) dp[i] = max(dp[i], 0);
for(int out: edges[i]) {
if(dp[out] < 0) continue;
dp[i] = max(dp[i], dp[out] + 1);
}
}
cout << dp[dest] << "\n";
}
void solve() {
int q;
{
int m;
cin >> n >> m >> q;
while(m--) {
int a, b;
cin >> a >> b;
edges[b].push_back(a);
}
}
for(int i = 1; i <= n; i++) {
map<int, int> cache;
cache[i] = 0;
for(int par: edges[i]) {
for(pii val: best[par]) {
cache[val.second] = max(cache[val.second], val.first + 1);
}
}
for(auto out: cache) {
best[i].emplace_back(out.second, out.first);
}
if(sz(best[i]) > SAVE) {
nth_element(best[i].begin(), best[i].begin() + sz(best[i]) - SAVE, best[i].end());
reverse(all(best[i]));
best[i].resize(SAVE);
}
sort(rall(best[i]));
}
for(int qq = 1; qq <= q; qq++) {
int dest;
cin >> dest;
int k;
cin >> k;
while(k--) {
int x;
cin >> x;
excluded[x] = qq;
}
int ret = -2;
bool found = false;
for(pii out: best[dest]) {
if(excluded[out.second] == qq) continue;
ret = out.first;
found = true;
break;
}
if(found) {
cout << ret << "\n";
continue;
}
solve(dest, qq);
}
}
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
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... |