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 ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e5+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n, m, q;
vector<int> adj2[MAXN];
vector<int> kisiler;
vector<pii> ans[MAXN];
bool visit[MAXN];
int d[MAXN];
const int sq = 150;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("../IO/int.txt","r",stdin);
freopen("../IO/out.txt","w",stdout);
#endif
cin>>n>>m>>q;
for(int i = 0; i < m; i++){
int a, b;
cin>>a>>b;
adj2[b].push_back(a);
}
for(int i = 1; i <= n; i++){
for(int &j: adj2[i]){
for(pii u: ans[j]){
d[u.ss] = max(d[u.ss], u.ff + 1);
}
}
for(int &j: adj2[i]){
for(pii &u: ans[j]){
if(d[u.ss] != -1){
ans[i].push_back({d[u.ss], u.ss});
d[u.ss] = -1;
}
}
}
ans[i].push_back({0, i});
sort(ans[i].begin(), ans[i].end(), greater<pii>());
while(ans[i].size() > sq){
ans[i].pop_back();
}
}
for(int i = 0; i < q; i++){
int x;
int k;
cin>>x>>k;
vector<int> kisi(k);
for(int j = 0; j < k; j++){
cin>>kisi[j];
visit[kisi[j]] = 1;
}
if(k >= sq){
for(int i = 1; i < x; i++) d[i] = -mod;
d[x] = 0;
int res = -1;
for(int i = x; i >= 1; i--){
if(d[i] < 0) continue;
if(d[i] > res && !visit[i]) res = d[i];
for(int &j: adj2[i]) d[j] = max(d[j], d[i] + 1);
}
cout<<res<<endl;
}else{
int res = -1;
for(int i = 0; i < ans[x].size(); i++){
if(visit[ans[x][i].ss]) continue;
res = ans[x][i].ff;
break;
}
cout<<res<<endl;
}
for(int j: kisi) visit[j] = 0;
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message (stderr)
bitaro.cpp: In function 'int main()':
bitaro.cpp:84:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ans[x].size(); i++){
~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |