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>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pi pair<int,int>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define alll(x) ((x).begin()+1), (x).end()
#define clean(v) (v).resize(distance((v).begin(), unique(all(v))));
#define yes cout<<"Yes"<<endl;
#define no cout<<"No"<<endl;
#define mod mod
#define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 998244353;
void io() {
ios::sync_with_stdio(false);
cin.tie(NULL);
}
template<class T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<class T>
bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
void nop() {
cout << -1 << endl;
return;
}
const int N = 1e5+5 , B = 350 ;
vector<int> adj[N] ;
vector<pi> best[N] ;
int vis[N] ;
vector<pi> merge(const vector<pi>& a , const vector<pi>& b)
{
vector<pi> tmp ;
int sa = a.size() , sb = b.size() ;
int i = 0 , j = 0 ;
while(tmp.size()<B+3 && i<sa && j<sb){
if(a[i].F>=b[j].F+1){
tmp.pb(a[i]) ;
vis[a[i].S] = 1 ;
i++ ;
}
else{
tmp.pb(make_pair(b[j].F +1 , b[j].S)) ;
vis[b[j].S] = 1 ;
j++ ;
}
while(i<sa && vis[a[i].S]) i++ ;
while(j<sb && vis[b[j].S]) j++ ;
}
while(tmp.size()<B+3 && j<sb){
tmp.pb(make_pair(b[j].F +1 , b[j].S)) ;
j++ ;
}
while(tmp.size()<B+3 && i<sa){
tmp.pb(a[i]) ;
i++ ;
}
for(auto p : tmp) vis[p.S] = 0 ;
return tmp ;
}
int marked[N] ;
int ans ;
void solve() {
int n , m , q ;
cin>>n>>m>>q ;
for(int i = 1 ; i<=m ; i++){
int u , v ;
cin>>u>>v ;
assert(u<v) ;
adj[v].pb(u) ;
}
for(int i = 1 ; i<=n ; i++){
best[i].pb({0 , i}) ;
for(int to : adj[i]){
best[i] = merge(best[i] , best[to]) ;
}
}
while(q--){
int t , y ;
cin>>t>>y ;
vector<int> v ;
for(int i = 1 ; i<=y ; i++){
int x ; cin>>x ;
marked[x] = 1 ;
v.pb(x) ;
}
ans = -1 ;
if(y<=B){
for(auto & i : best[t]){
if(!marked[i.S]){
ans = i.F ;
break ;
}
}
}
else{
queue<pi> q ; q.push({t , 0}) ;
vector<int> done(n+1 , -1) ;
done[t] = 0 ;
for(int i = n ; i ; i--){
if(done[i]==-1) continue;
for(int to : adj[i]){
ckmax(done[to] , done[i] + 1) ;
}
if(!marked[i]) ckmax(ans , done[i]) ;
}
}
cout<<ans<<endl;
for(int j : v) marked[j] = 0 ;
}
}
int main() {
io();
ll tt = 1;
//cin>>tt ;
while (tt--) {
solve();
}
return 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... |