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 <iostream>
#include <tuple>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <random>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cctype>
#include <climits>
#include <chrono>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
template <typename T>
void pr(vector<T> &v) {
FOR(i, 0, sz(v)) cout << v[i] << " ";
cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
cin >> x;
}
template <typename T>
void re(vector<T> &a) {
FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
re(first);
re(rest...);
}
template <typename T>
void pr(T x) {
cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
cout << first << " ";
pr(rest...);
cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
const ll MOD = 1000000007;
#define inf 1e18;
#define INF INT_MAX
long double PI = 4*atan(1);
long double eps = 1e-16;
int n,m,q;
vvi adj(100005);
vvi rev(100005);
vector<pii> chain[100005];
int bad[100005] = {};
int dp[100005] = {};
int BLOCK = 300;
void precomp() {
vi seen(100005);
FOR(i,0,n) {
chain[i].pb({0, i});
for(auto v : rev[i]) {
vector<pii> curr;
int ap = 0;
int bp = 0;
int asize = chain[i].size();
int bsize = chain[v].size();
while(curr.size() < BLOCK && (ap < asize || bp < bsize)) {
if(bp == bsize || (chain[i][ap].ff >= chain[v][bp].ff + 1)) {
curr.pb(chain[i][ap]);
seen[chain[i][ap].ss] = 1;
ap++;
}
else {
curr.pb({chain[v][bp].ff + 1, chain[v][bp].ss});
seen[chain[v][bp].ss] = 1;
bp++;
}
while(ap < asize && seen[chain[i][ap].ss]) ap++;
while(bp < bsize && seen[chain[v][bp].ss]) bp++;
}
swap(chain[i], curr);
for(pii x : chain[i]) seen[x.ss] = 0;
}
}
}
void brute(int t) {
FOR(i,0,t+1) dp[i] = -1e9;
dp[t] = 0;
RFOR(i,t, 0) {
for(auto v : adj[i]) {
if(v <= t) {
dp[i] = max(dp[i], dp[v] + 1);
}
}
}
int ans = -1;
FOR(i,0,t+1) {
if(dp[i] >= 0 && bad[i] == 0) ans = max(ans, dp[i]);
}
cout << ans << endl;
}
void solve() {
int t, y; cin >> t >> y; t--;
vi busy(y);
FOR(i,0,y) {
cin >> busy[i];
busy[i]--;
bad[busy[i]] = 1;
}
if(y >= BLOCK) {
brute(t);
} else {
int ans = -1;
for(auto x : chain[t]) {
if(bad[x.ss] == 0) ans = max(ans, x.ff);
}
cout << ans << endl;
}
FOR(i,0,y) {
bad[busy[i]] = 0;
}
}
int main() {
auto start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);cin.tie(0);
//ofstream cout("output.txt");
//ifstream cin("input.txt");
#ifdef DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> n >> m >> q;
FOR(i,0,m) {
int u,v; cin >> u >> v; u--; v--;
adj[u].pb(v);
rev[v].pb(u);
}
precomp();
FOR(i,0,q) {
solve();
}
// cout << 100000 << " " << 200000 << " " << 300 << endl;
// FOR(i,0,99999) {
// cout << i + 1 << " " << i + 2 << endl;
// }
// FOR(i,0,99998) {
// cout << i + 1 << " " << i + 3 << endl;
// }
// cout << 567 << " " << 2921 << endl;
// cout << 1252 << " " << 89592 << endl;
// cout << 788 << " " << 790 << endl;
// FOR(i,0,300) {
// cout << 300 * i + 1<< " " << 300 << " ";
// FOR(j,0,300) {
// cout << i * 300 + j + 1<< " ";
// }
// cout << endl;
// }
// auto stop = chrono::high_resolution_clock::now();
// auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
// cout << duration.count() << endl;
//cin.close();
//cout.close();
}
Compilation message (stderr)
bitaro.cpp: In function 'void precomp()':
bitaro.cpp:116:31: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
116 | while(curr.size() < BLOCK && (ap < asize || bp < bsize)) {
| ~~~~~~~~~~~~^~~~~~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:183:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
183 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |