Submission #1103268

#TimeUsernameProblemLanguageResultExecution timeMemory
1103268hiensumiBitaro’s Party (JOI18_bitaro)C++14
100 / 100
1183 ms146228 KiB
#include <bits/stdc++.h>
using namespace std;
#define fod(i,a,b) for(int i((int) (a)), _b(b); i <= _b; i++)
#define fok(i,a,b) for(int i((int) (a)), _b(b); i >= _b; i--)
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define ve vector
#define vi ve<int>
#define vll ve<ll>
#define el '\n'
#define mask(i) (1LL<<(i))
#define BIT(msk,i) (msk>>(i)&1LL)
template<class T> bool mini(T &a, T b){ return (a > (b) ? a = (b), 1 : 0); }
template<class T> bool maxi(T &a, T b){ return (a < (b) ? a = (b), 1 : 0); }
const int base = mask(20) + 5;
#define name "bitaro_party"

int n, m, q;

const int N = 1e5;
const int M = 2e5;
const int Q = 1e5;
const int SQRTN = 100;

ve <vi> g;

int dp[N + 5];

namespace sub1{
    bool check(){
        return (n <= 1000 and m <= 2000) or q == 1;
    }

    int host, num;

    void solve(){

        while(q--){
            cin >> host >> num;

          	fod(i,1,host) dp[i] = 0;

            fod(i,1,num){
                int x; cin >> x;
                dp[x] = -1e9;
            }

            fod(i,1,host){
                for(int j : g[i]){
                    maxi(dp[i], dp[j] + 1);
                }
            }

            cout << max(dp[host], -1) << el;
        }
    }
}

namespace sub2{
	int dis[N + 5];
    ve <pii> f[N + 5];

    int host, num;
    int cant[N + 5];

    bool del[N + 5];

    int solve_light(){
        for(pii x : f[host]) if(!del[x.se]) return x.fi;
        return -1;
    }

    int solve_heavy(){
        fod(i,1,host) dp[i] = 0;

        fod(i,1,num) dp[cant[i]] = -1e9;

        fod(i,1,host){
            for(int j : g[i]){
                maxi(dp[i], dp[j] + 1);
            }
        }

        return max(dp[host], -1);
    }


    void solve(){
        fod(i,1,n){
        	bitset <N+5> vst;

            for(int j : g[i]){
            	for(pii path : f[j]){
            		maxi(dis[path.se], path.fi);
            		vst.set(path.se);
            	}
            }

            f[i].pb(mp(0, i));
            for(int x = vst._Find_first(); x < (int)vst.size(); x = vst._Find_next(x)){
            	f[i].pb(mp(dis[x] + 1, x));
            	dis[x] = -1;
            }
        	sort(f[i].begin(), f[i].end(), greater<pii>());
        	while((int) f[i].size() > SQRTN) f[i].pop_back();
        }

        while(q--){
            cin >> host >> num;
            fod(i,1,num) cin >> cant[i];

            fod(i,1,num) del[cant[i]] = 1;

            if(num < SQRTN) cout << solve_light() << el;
            else cout << solve_heavy() << el;

            fod(i,1,num) del[cant[i]] = 0;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    if(fopen(name".inp", "r")){
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }

    cin >> n >> m >> q;

    {
        int u, v;
        g.resize(n + 1);
        fod(i,1,m){
            cin >> u >> v;
            g[v].pb(u);
        }
    }

    fod(i,1,n) sort(g[i].begin(), g[i].end());

    if(sub1 :: check()) sub1 :: solve();
	else sub2 :: solve();

    return 0;
}

Compilation message (stderr)

bitaro.cpp: In function 'int main()':
bitaro.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...