This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |