Submission #1193787

#TimeUsernameProblemLanguageResultExecution timeMemory
1193787ortsacBitaro’s Party (JOI18_bitaro)C++17
0 / 100
2099 ms131064 KiB
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fst first
#define snd second
using namespace std;
 
vector<vector<int>> adj;
vector<vector<int>> rAdj;
vector<vector<pair<int,int>>> yth;
 
vector<pair<int,int>> aux;
vector<int> vst;
void merge(int i, int j) {
    aux.clear();
    int l=0, maxl = yth[i].size()-1;
    int r=0, maxr = yth[j].size()-1;
    for (auto& u : yth[i]) u.first++;
    vst.clear(), vst.resize(i+1,0);
 
    int f, s;
    while(aux.size()<=301) {
        if(l<=maxl and r<=maxr) {
            if(yth[i][l].fst > yth[j][r].fst) {
                f=yth[i][l].fst, s=yth[i][l].snd;
                if(!vst[s]) {
                    vst[s]=1;
                    aux.pb({f,s});
                }
                l++;
            } else {
                f=yth[j][r].fst, s=yth[j][r].snd;
                if(!vst[s]) {
                    vst[s]=1;
                    aux.pb({f,s});
                }
                r++;
            }
        } else if(l<=maxl) {
            f=yth[i][l].fst, s=yth[i][l].snd;
            if(!vst[s]) {
                vst[s]=1;
                aux.pb({f,s});
            }
            l++;
        } else if(r<=maxr) {
            f=yth[j][r].fst, s=yth[j][r].snd;
            if(!vst[s]) {
                vst[s]=1;
                aux.pb({f,s});
            }
            r++;
        } else break;
    }
    yth[j]=aux;
    for (auto& u : yth[i]) u.first--;
}
void precalc(int n) {
    for(int x=1; x<=n; ++x) {
        yth[x].pb({0,x});
        for(int y : rAdj[x]) {
            merge(x,y);
        }
    }
}
 
vector<int> busy;
int solve(int t) {
    for(int i=0; i<=(yth[t].size()-1); ++i) {
        if(!busy[yth[t][i].snd]) return yth[t][i].fst;
    }
    return -1;
}
queue<int> prox;
vector<int> d;
int brute(int t) {
    prox.push(t);
    d.clear(), d.resize(t+1,0);
 
    int ans=-1, at;
    while(!prox.empty()) {
        at=prox.front(), prox.pop();
        if(!busy[at]) ans=at;
        for(int to : adj[at]) {
            d[to]=d[at]+1;
            prox.push(to);
        }
    }
 
    if(ans==-1) return -1;
    return d[ans];
}
 
int32_t main() {
    int n, m, q; cin >> n >> m >> q;
    adj.resize(n+1); yth.resize(n+1); rAdj.resize(n + 1);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        adj[b].pb(a);
        rAdj[a].pb(b);
    }
    precalc(n);
    //for (int i = 1; i <= n; i++) {
    //    cout << i << "\n";
    //    for (auto u : yth[i]) cout << u.first << " " << u.second << "\n";
    //}
    //cout << "Oi\n";
    while(q--) {
        int t, y;
        cin >> t >> y;
        busy.clear(), busy.resize(n+1,0);
        for(int i=0; i<y; ++i) {
            int a;
            cin >> a;
            busy[a]=1;
        }
 
        if(y<=300) cout << solve(t) << endl;
        else cout << brute(t) << endl;
        // if(brute(t)!=solve(t)) {
        //     cout << t << endl;
        //     // for(int i=1; i<=n; ++i) {
        //     //     for(int j : adj[i]) cout << j << " ";
        //     //     if(!adj[i].empty()) cout << endl << i << endl;
        //     // }
        //     for(int i=1; i<=n; ++i) {
        //         if(busy[i]) cout << i << " ";
        //     }
        //     cout << endl;
        //     cout << "opa, aq amigao: " << brute(t) << " " << solve(t) << endl;
        // } else cout << "auuuh " << solve(t) << endl;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...