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 optimize("Ofast")
//#pragma GCC optimize("trapv")
#define st first
#define nd second
#define pb push_back
#define pp pop_back
#define eb emplace_back
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename H, typename T>
ostream& operator<<(ostream& os, pair<H, T> m){
return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H>
ostream& operator<<(ostream& os, vector<H> V){
os<<"{";
for(int i=0; i<V.size(); i++){
if(i)os<<" ";
os<<V[i];
}
os<<"}";
return os;
}
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1e5+5, INF=1e9+5, mod=1e9+7, K=120;
vi best[N], dist[N];
int ans[N];
int czy[N];
vi E[N];
int main(){
BOOST;
int n, m, q;
cin>>n>>m>>q;
for(int i=0; i<m; i++){
int a, b;
cin>>a>>b;
E[b].pb(a);
}
for(int v=1; v<=n; v++){
vector<int> wsk(E[v].size());
while(best[v].size()<K){
int opt=0, d=-1;
for(int i=0; i<E[v].size();i++){
while(wsk[i]<best[E[v][i]].size() && czy[best[E[v][i]][wsk[i]]])wsk[i]++;
if(wsk[i]<best[E[v][i]].size() && dist[E[v][i]][wsk[i]]>d){
opt=i;
d=dist[E[v][i]][wsk[i]];
}
}
if(d==-1){
best[v].pb(v);
dist[v].pb(0);
break;
}
else{
best[v].pb(best[E[v][opt]][wsk[opt]]);
dist[v].pb(1+dist[E[v][opt]][wsk[opt]]);
czy[best[v].back()]=1;
}
}
for(int i:best[v])czy[i]=0;
//deb(v, best[v], dist[v], E[v]);
}
while(q--){
int v, k;
cin>>v>>k;
vi V(k);
for(int &i:V)cin>>i, czy[i]=1;
if(k<K){
bool b=0;
for(int i=0; i<best[v].size(); i++){
if(!czy[best[v][i]]){
cout<<dist[v][i]<<"\n";
b=1;
break;
}
}
if(!b)cout<<"-1\n";
}
else{
for(int i=1; i<=v; i++){
ans[i]=-INF;
if(!czy[i])ans[i]=0;
for(int j:E[i]){
ans[i]=max(ans[i], ans[j]+1);
}
}
if(ans[v]<0)cout<<-1<<"\n";
else cout<<ans[v]<<"\n";
}
for(int i:V)czy[i]=0;
}
}
Compilation message (stderr)
bitaro.cpp: In function 'int main()':
bitaro.cpp:76:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i=0; i<E[v].size();i++){
| ~^~~~~~~~~~~~
bitaro.cpp:77:17: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | while(wsk[i]<best[E[v][i]].size() && czy[best[E[v][i]][wsk[i]]])wsk[i]++;
bitaro.cpp:78:14: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | if(wsk[i]<best[E[v][i]].size() && dist[E[v][i]][wsk[i]]>d){
bitaro.cpp:104:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i=0; i<best[v].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... |