Submission #809184

#TimeUsernameProblemLanguageResultExecution timeMemory
809184setytousiBitaro’s Party (JOI18_bitaro)C++14
100 / 100
1120 ms427020 KiB
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <sstream>
#include <numeric>
#include <cstdio>
#include <bitset>
#include <math.h>
#include <assert.h>
 
using namespace std;
 
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
 
const ll MAXN = 2e5 + 10;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll SQ = 320;
 
#define fast_io     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define pb          push_back
#define F           first
#define S           second
#define Sz(x)       int((x).size())
#define all(x)      (x).begin(), (x).end()
#define cl          clear()
#define endll       '\n'

vector<int> adj[MAXN], adj2[MAXN];
vector<pii> vec[MAXN];
vector<pii> tmp;
vector<int> busy;
bool mark[MAXN];
int dp[MAXN];

int main(){
    fast_io;
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < m; i++){
        int v, u;
        cin >> v >> u;
        adj[v].pb(u);
        adj2[u].pb(v);
    }
    for (int v = 1; v <= n; v++){
        vec[v].pb({0, v});
        for (auto u : adj2[v]){
            tmp.cl;
            int p1 = 0, p2 = 0;
            while(p1 < Sz(vec[u]) || p2 < Sz(vec[v])){
                if (p1 < Sz(vec[u]) && mark[vec[u][p1].S]){
                    p1++;
                    continue;
                }
                if (p2 < Sz(vec[v]) && mark[vec[v][p2].S]){
                    p2++;
                    continue;
                }
                if (p1 >= Sz(vec[u])) tmp.pb(vec[v][p2]), mark[vec[v][p2].S] = 1, p2++;
                else if (p2 >= Sz(vec[v])) tmp.pb({vec[u][p1].F + 1, vec[u][p1].S}), mark[vec[u][p1].S] = 1, p1++;
                else if (vec[u][p1].F + 1 >= vec[v][p2].F) tmp.pb({vec[u][p1].F + 1, vec[u][p1].S}), mark[vec[u][p1].S] = 1, p1++;
                else tmp.pb(vec[v][p2]), mark[vec[v][p2].S] = 1, p2++;
            }
            vec[v].cl;
            for (auto i : tmp) mark[i.S] = 0;
            while(Sz(tmp) > SQ) tmp.pop_back();
            for (auto i : tmp) vec[v].pb(i);
        }
    }
    while(q--){
        int t, y;
        cin >> t >> y;
        if (y < SQ){
            for (int i = 0; i < y; i++){
                int v;
                cin >> v;
                mark[v] = 1;
                busy.pb(v);
            }
            int ans = -1;
            for (auto u : vec[t]){
                if (!mark[u.S]){
                    ans = max(ans, u.F);
                }
            }
            cout << ans << endll;
            for (auto i : busy) mark[i] = 0;
            busy.cl;
        }
        else{
            for (int i = 0; i <= n; i++) dp[i] = -1000000000, mark[i] = 0;
            for (int i = 0; i < y; i++){
                int v;
                cin >> v;
                mark[v] = 1;
            }
            int ans = -1;
            dp[t] = 0;
            for (int v = t; v >= 1; v--){
                for (auto u : adj[v]){
                    dp[v] = max(dp[v], dp[u] + 1);
                }
                if (mark[v] != 1) ans = max(ans, dp[v]);
            }
            cout << ans << endll;
            for (int i = 0; i <= n; i++) mark[i] = 0;
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...