제출 #1346774

#제출 시각아이디문제언어결과실행 시간메모리
1346774tuanvn2009Bitaro’s Party (JOI18_bitaro)C++17
0 / 100
4 ms5444 KiB
#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

#include <cmath>

const int maxn = 1e5 + 3, maxs = 100;

struct Path {
    int v, dist;
};

vector<int> g[maxn];
vector<Path> topK[maxn];

bool exist[maxn], invalid[maxn];
int dist[maxn];

void preprocess(int u) {
    topK[u] = {{u, 0}};
    
    for (int &v : g[u]) {
        auto a = topK[u], b = topK[v];
        
        for (auto &path : a) exist[path.v] = false;
        for (auto &path : b) exist[path.v] = false;
        
        topK[u].resize(0);
        
        while (topK[u].size() < maxs + 3) {
            Path curA = {-1, 0}, curB = {-1, -1};
            
            while (a.size() && exist[a.back().v]) a.pop_back();
            while (b.size() && exist[b.back().v]) b.pop_back();
            
            if (a.size()) curA = a.back();
            if (b.size()) curB = b.back();
            
            if (curA.dist < ++curB.dist) swap(curA, curB);
            if (curA.v < 0) break;
            
            topK[u].push_back(curA);
            exist[curA.v] = true;
        }
        
        reverse(begin(topK[u]), end(topK[u]));
    }
}

int maxDist(int u) {
    int res = -1;
    dist[u] = 0;
    
    for (int i = u; i; --i) for (int &v : g[i]) {
        dist[v] = dist[i] + 1;
        
        if (!invalid[v]) {
            res = max(res, dist[v]);
        }
    }
    
    return res;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    static char buf[1 << 20];
    cout.rdbuf() -> pubsetbuf(buf, sizeof(buf));
    
    int n, m, q; cin >> n >> m >> q;
    
    while (m--) {
        int u, v; cin >> u >> v;
        if (u < v) swap(u, v);
        
        g[u].push_back(v);
    }
    
    for (int i = 1; i <= n; ++i) preprocess(i);
    for (auto &u : topK) reverse(begin(u), end(u));
    
    while (q--) {
        int t, y; cin >> t >> y;
        vector<int> c(y);
        
        for (int &u : c) {
            cin >> u;
            invalid[u] = true;
        }
        
        if (y < maxs) for (auto &cur : topK[t]) {
            if (!invalid[cur.v]) {
                cout << cur.dist << '\n';
                goto skip;
            }
        } else {
            cout << maxDist(t) << '\n';
            goto skip;
        }
        
        cout << -1 << '\n';
        skip: true;
        
        for (int &u : c) {
            invalid[u] = false;
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...