답안 #579698

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579698 2022-06-19T16:11:04 Z jasmin Event Hopping (BOI22_events) C++17
0 / 100
259 ms 53796 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;
vector<int> tin;
vector<int> tout;
int t=0;

void dfs(int v, vector<bool>& vis, vector<vector<int> >& adi){
    if(vis[v]) return;
    vis[v]=true;
    tin[v]=t;
    t++;

    for(auto u: adi[v]){
        dfs(u, vis, adi);
    }

    tout[v]=t;
    t++;
}

bool ancestor(int a, int b){
    return tin[a]<=tin[b] && tout[b]<=tout[a];
}

int dist(int a, int b, vector<vector<int> >& up, int l){
    if(!ancestor(b, a)) return inf;

    int ans=0;
    for(int x=l-1; x>=0; x--){
        if(up[a][x]!=-1 && ancestor(b, up[a][x])){
            ans+=(1<<x);
            a=up[a][x];
        }
    }
    assert(up[a][0]==b);
    return ans+1;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, q;
    cin >> n >> q;
    vector<pair<int,int> > e(n);
    map<int, pair<vector<int>, vector<int> > > time;
    for(int i=0; i<n; i++){
        int s, t;
        cin>> s >> t;
        e[i]={s, t};
        time[s].first.push_back(i);
        time[t].second.push_back(i);
    }

    vector<vector<int> > adi(n);
    vector<int> p(n, -1);
    set<int> active;
    for(auto m: time){
        for(auto e: m.second.first){
            active.insert(e);
        }
        
        for(auto e: m.second.second){
            for(auto x: active){
                if(x==e) continue;
                p[e]=x;
                adi[x].push_back(e);
            }
        }
        for(auto e: m.second.second){
            active.erase(e);
        }
    }

    int l=25;
    vector<vector<int> > up(n, vector<int> (l, -1));
    for(int i=0; i<n; i++){
        up[i][0]=p[i];
    }
    for(int x=1; x<l; x++){
        for(int i=0; i<n; i++){
            if(up[i][x-1]!=-1){
                up[i][x]=up[up[i][x-1]][x-1];
            }
        }
    }

    tin.resize(n);
    tout.resize(n);
    vector<bool> vis(n);
    for(int i=0; i<n; i++){
        if(p[i]==-1){
            dfs(i, vis, adi);
        }
    }

    for(int i=0; i<q; i++){
        int a, b;
        cin >> a >> b;
        int ans=dist(b-1, a-1, up, l);
        if(ans==inf){
            cout << "impossible\n";
        }
        else{
            cout << ans << "\n";
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 259 ms 53796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -