#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;
}
if(n > 1000 || q > 100){
while(q--){
int i, j; cin >> i >> j;
if(i == j){
cout << 0 << '\n';
}
else if(v[i].second <= v[j].second && v[i].second >= v[j].first){
cout << 1 << '\n';
}
else{
cout << "impossible\n";
}
}
return 0;
}
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 |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
280 ms |
3148 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
232 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
340 KB |
Output is correct |
5 |
Correct |
6 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
1589 ms |
1108 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
232 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
340 KB |
Output is correct |
5 |
Correct |
6 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
1589 ms |
1108 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
232 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
340 KB |
Output is correct |
5 |
Correct |
6 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
1589 ms |
1108 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
296 ms |
2096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
280 ms |
3148 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |