제출 #714282

#제출 시각아이디문제언어결과실행 시간메모리
714282vjudge1Event Hopping (BOI22_events)C++17
0 / 100
1563 ms8628 KiB
#include <bits/stdc++.h> #define ll long long #define endl '\n' using namespace std; const int MAX = 3e5 + 5; vector<int> adj[100001]; int color[100001], dis[100001]; void bfs(int s){ queue<int> q; q.push(s); dis[s] = 0; color[s] = 1; while(!q.empty()){ int u = q.front(); q.pop(); for(auto v : adj[u]){ if(color[v]) continue; color[v] = 1; dis[v] = dis[u] + 1; q.push(v); } } } struct event{ int s, e, i; bool operator<(event other){ if(e == other.e){ return s < other.s; } return e < other.e; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin>>n>>q; vector<int> S(n + 1), E(n + 1); vector<event> arr; for (int i = 1; i <= n; i++) { cin>>S[i]>>E[i]; arr.push_back({S[i], E[i], i}); } sort(arr.begin(), arr.end()); for(int i = 0; i < n - 1; i++){ int first = arr[i].i; int second = arr[i + 1].i; if(S[second] <= E[first] && E[first] <= E[second]) adj[first].push_back(second); } while(q--){ memset(color, 0, sizeof(color)); memset(dis, -1, sizeof(dis)); int s, e; cin>>s>>e; bfs(s); if(dis[e] != -1) cout<<dis[e]<<endl; else cout<<"impossible\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...