Submission #1314625

#TimeUsernameProblemLanguageResultExecution timeMemory
1314625m.h.nBitaro’s Party (JOI18_bitaro)C++20
0 / 100
1 ms568 KiB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M,Q;
    if(!(cin>>N>>M>>Q)) return 0;
    int k = (int)sqrt(N) + 1;

    vector<vector<int>> add(N+1), rem(N+1);
    for(int i=0;i<M;i++){
        int s,e; cin>>s>>e;
        add[s].push_back(e);
        rem[e].push_back(s);
    }

   
    vector<vector<pii>> st(N+1);
  
    vector<int> seen(N+1, 0), best(N+1, 0);

   
    for(int v=1; v<=N; ++v){
        vector<int> list_srcs; list_srcs.reserve( max(1, (int)rem[v].size()*min(k,5)) );

        
        for(int u : rem[v]){
            auto &su = st[u];
            for(auto &p : su){ 
                int src = p.second;
                int val = p.first + 1;
                if(seen[src] != v){
                    seen[src] = v;
                    best[src] = val;
                    list_srcs.push_back(src);
                } else if(best[src] < val){
                    best[src] = val;
                }
            }
      
            if(seen[u] != v){
                seen[u] = v;
                best[u] = 1;
                list_srcs.push_back(u);
            } else if(best[u] < 1){
                best[u] = 1;
            }
        }

        
        if(seen[v] != v){
            seen[v] = v;
            best[v] = 0;
            list_srcs.push_back(v);
        } else if(best[v] < 0){
            best[v] = 0;
        }

        
        vector<pii> items; items.reserve(list_srcs.size());
        for(int src : list_srcs) items.emplace_back(best[src], src);

        if((int)items.size() > k){
            auto cmp = [](const pii &a, const pii &b){ return a.first > b.first; }; 
            nth_element(items.begin(), items.begin()+k, items.end(), cmp);
            items.resize(k);
            sort(items.begin(), items.end(), cmp);
        } else {
            sort(items.begin(), items.end(), [](const pii &a, const pii &b){ return a.first > b.first; });
        }

        st[v].swap(items); 
    }

  
    vector<int> dp(N+1, -1000000000);
    while(Q--){
        int T, Y;
        cin >> T >> Y;
        vector<int> busy(Y);
        for(int i=0;i<Y;i++) cin>>busy[i];

        if(Y < k){
            
            unordered_set<int> busyset;
            busyset.reserve(Y*2+1);
            for(int b : busy) busyset.insert(b);
            int ans = -1000000000;
            for(auto &p : st[T]){
                if(busyset.find(p.second) == busyset.end()){
                    ans = max(ans, p.first);
                    break; 
                }
            }
            if(ans == -1000000000) cout << -1 << '\n';
            else cout << ans << '\n';
        } else {
    
            for(int i=1;i<=N;i++) dp[i] = -1000000000;
            dp[T] = 0;
            for(int x=T; x>=1; --x){
                if(dp[x] == -1000000000) continue;
                for(int u : rem[x]){  } 
                for(int v : add[x]){
                    if(dp[v] != -1000000000){
                       
                    }
                }
              
                int bestx = dp[x];
                for(int v: add[x]){
                    if(dp[v] != -1000000000) bestx = max(bestx, dp[v] + 1);
                }
                dp[x] = bestx;
            }
    
            vector<char> isbusy(T+1, 0);
            for(int b: busy) if(b<=T) isbusy[b]=1;
            int ans = -1000000000;
            for(int i=1;i<=T;i++){
                if(!isbusy[i]) ans = max(ans, dp[i]);
            }
            if(ans == -1000000000) cout << -1 << '\n';
            else cout << ans << '\n';
        }
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...