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>
#define ll long long
#define pii pair<int, int>
#define oo 1e9
using namespace std;
vector<vector<int>> g;
vector<bool> visited;
int dfs(int node, int target){
if(node == target) return 0;
visited[node] = 1;
int ans = oo;
for(int to:g[node]){
if(visited[to]) continue;
ans = min(ans, dfs(to, target) + 1);
}
visited[node] = 0;
return ans;
}
int main(){
int n, q; cin >> n >> q;
vector<pii> v(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> v[i].first >> v[i].second;
}
visited.resize(n + 1, 0);
g.resize(n + 1);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if(i == j) continue;
if(v[i].second <= v[j].second && v[i].second >= v[j].first){
g[i].emplace_back(j);
}
}
}
while(q--){
int u, v; cin >> u >> v;
int ans = dfs(u, v);
if(ans == oo){
cout << "impossible\n";
}
else{
cout << ans << '\n';
}
}
}
# | 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... |