Submission #1086650

#TimeUsernameProblemLanguageResultExecution timeMemory
1086650SuPythonyEvent Hopping (BOI22_events)C++17
10 / 100
1595 ms31048 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<vector<int>> al; int main() { int n,q; cin>>n>>q; vector<pair<int,int>> ev; for (int i=0; i<n; i++) { int s,e; cin>>s>>e; ev.push_back({s,e}); } al.assign(n+1,vector<int>()); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (i==j) continue; if (ev[j].first<=ev[i].second&&ev[i].second<=ev[j].second) { al[i+1].push_back(j+1); } } } while (q--) { int s,e; cin>>s>>e; queue<pair<int,int>> q; q.push({s,0}); vector<int> vis(n+1,0); vis[s]=1; int ans=-1; if (s==e) ans=0; while (!q.empty()) { auto t=q.front(); q.pop(); bool f=false; for (int v: al[t.first]) { if (v==e) { ans=t.second+1; f=true; break; } else { if (!vis[v]) { vis[v]=1; q.push({v,t.second+1}); } } } if (f) break; } if (ans==-1) cout<<"impossible\n"; else cout<<ans<<"\n"; } return 0; }
#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...