Submission #1296493

#TimeUsernameProblemLanguageResultExecution timeMemory
1296493trandaihao5555Bitaro’s Party (JOI18_bitaro)C++20
7 / 100
696 ms524796 KiB
#include <bits/stdc++.h>

#define int long long

#define debug     cout << "ok\n";
#define SQR(x)    (1LL * ((x) * (x)))
#define MASK(i)   (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi        first
#define se        second
#define pb        push_back

#define mp make_pair
#define pii pair<int,int>
#define pli pair<ll,int>
#define vi vector<int>

#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui;

using namespace std;

const int M = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);

const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};

template<class _, class __>
    bool minimize(_ &x, const __ y){
        if(x > y){
            x = y;
            return true;
        } else return false;
    }
template<class _, class __>
    bool maximize(_ &x, const __ y){
        if(x < y){
            x = y;
            return true;
        } else return false;
    }

template<class _,class __>
    void Add(_ &x, const __ y) {
        x += y;
        if (x >= M) {
            x -= M;
        }
        return;
    }

template<class _,class __>
    void Diff(_ &x, const __ y) {
        x -= y;
        if (x < 0) {
            x += M;
        }
        return;
    }

//--------------------------------------------------------------

const int MaxN = 1e5+7;
const int S = 317;

int n,q,m,cnt,vis[MaxN],C[MaxN],f[MaxN];
vi a[MaxN];
vector<pii> lis[MaxN];

vector<pii> operator + (vector<pii> a,vector<pii> b) {
    cnt++;
    vector<pii> res;
    int p1 = 0;
    int p2 = 0;
    while (res.size() < S && p1 < a.size() && p2 < b.size()) {
        if (vis[a[p1].se] == cnt) p1++;
        else if (vis[b[p2].se] == cnt) p2++;
        else {
            if (a[p1].fi < b[p2].fi) {
                res.pb(b[p2]);
                vis[b[p2].se] = cnt;
                p2++;
            }
            else {
                res.pb(a[p1]);
                vis[a[p1].se] = cnt;
                p1++;
            }
        }
    }
    while (res.size() < S && p1 < a.size()) {
        if (vis[a[p1].se] == cnt) p1++;
        else {
            res.pb(a[p1]);
            vis[a[p1].se] = cnt;
            p1++;
        }
    }
    while (res.size() < S && p2 < b.size()) {
        if (vis[b[p2].se] == cnt) p2++;
        else {
            res.pb(b[p2]);
            vis[b[p2].se] = cnt;
            p2++;
        }
    }
    return res;
}

void sol() {
    cin >> n >> m >> q;
    for (int i=1;i<=m;i++) {
        int u,v;
        cin >> u >> v;
        a[v].pb(u);
    }
    for (int i=1;i<=n;i++) {
        for (int v : a[i]) {
            lis[i] = lis[i] + lis[v];
        }
        for (pii & x : lis[i]) x.fi++;
        if (lis[i].size() < S) lis[i].pb(mp(0,i));
    }
    for (int i=1;i<=n;i++) vis[i] = 0;
    for (int i=1;i<=q;i++) {
        int T,Y;
        cin >> T >> Y;
        for (int j=1;j<=Y;j++) {
            cin >> C[j];
            vis[C[j]] = i;
        }
        if (Y < S) {
            bool ch = true;
            for (pii tmp : lis[T]) {
                if (vis[tmp.se] != i) {
                    cout << tmp.fi << '\n';
                    ch = false;
                    break;
                }
            }
            if (ch) cout << -1 << '\n';
        }
        else {
            for (int j=1;j<=n;j++) {
                if (vis[j] != i) {
                    f[j] = 0;
                }
                else f[j] = -INF;
                for (int v : a[j]) {
                    maximize(f[j],f[v] + 1);
                }
            }
            if (f[T] >= 0) cout << f[T] << '\n';
            else cout << -1 << '\n';
        }
    }
}

signed main() {
//    freopen("test.inp","r",stdin);
//    freopen("test.out","w",stdout);
    FAST
    int t=1;
//    cin >> t;
    while (t--) sol();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...