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 pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int sq;
vector<bool> bl;
void merge(vector<pair<int, int>> &a, vector<pair<int, int>> &b){
vector<pair<int, int>> c;
for(int aa = 0, bb = 0; aa < a.size() || bb < b.size();){
if(aa >= a.size()){
if(!bl[b[bb].sc]) bl[b[bb].sc] = 1, c.pb(b[bb]);
bb++;
}
else if(bb >= b.size()){
if(!bl[a[aa].sc]) bl[a[aa].sc] = 1, c.pb(a[aa]);
aa++;
}
else if(a[aa].f >= b[bb].f){
if(!bl[a[aa].sc]) bl[a[aa].sc] = 1, c.pb(a[aa]);
aa++;
}
else{
if(!bl[b[bb].sc]) bl[b[bb].sc] = 1, c.pb(b[bb]);
bb++;
}
}
for(int i = 0; i < c.size(); i++) bl[c[i].sc] = 0;
b = c;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q; cin >> n >> m >> q;
bl.resize(n, 0);
sq = sqrt(n+0.0);
vector<vector<int>> v(n);
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b; a--, b--;
v[a].pb(b);
}
vector<vector<pair<int, int>>> dps(n);
for(int i = 0; i < n; i++) dps[i].pb({0, i});
for(int i = 0; i < n; i++){
vector<pair<int, int>> mg;
for(auto [c, nd]: dps[i]) mg.pb({c+1, nd});
for(int x: v[i]) merge(mg, dps[x]);
}
vector<bool> busy(n, 0);
while(q--){
int tj, yj; cin >> tj >> yj;
tj--;
vector<int> cbs(yj);
for(int &x: cbs) cin >> x, x--, busy[x] = 1;
if(yj <= sq){
int ans = !busy[tj];
ans--;
for(auto [c, nd]: dps[tj]) if(!busy[nd]) ans = max(ans, c);
cout << ans << "\n";
for(int x: cbs) busy[x] = 0;
continue;
}
vector<int> dp(n, -1);
for(int i = 0; i < n; i++){
if(busy[i] && dp[i] == -1) continue;
dp[i] = max(dp[i], 0);
for(int x: v[i]) dp[x] = max(dp[x], dp[i]+1);
}
cout << dp[tj] << "\n";
for(int x: cbs) busy[x] = 0;
}
}
Compilation message (stderr)
bitaro.cpp: In function 'void merge(std::vector<std::pair<int, int> >&, std::vector<std::pair<int, int> >&)':
bitaro.cpp:12:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for(int aa = 0, bb = 0; aa < a.size() || bb < b.size();){
| ~~~^~~~~~~~~~
bitaro.cpp:12:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for(int aa = 0, bb = 0; aa < a.size() || bb < b.size();){
| ~~~^~~~~~~~~~
bitaro.cpp:13:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | if(aa >= a.size()){
| ~~~^~~~~~~~~~~
bitaro.cpp:17:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | else if(bb >= b.size()){
| ~~~^~~~~~~~~~~
bitaro.cpp:30:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 0; i < c.size(); i++) bl[c[i].sc] = 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... |