#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define sz(x) int((x).size())
#define pb push_back
#define s second
#define f first
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
const int INF=1e9;
const int N=5010;
int n, q, s[N], e[N], d[N][N];
vi ad[N];
int main()
{
FAST_IO;
cin >> n >> q;
FOR(i, 0, n)
{
cin >> s[i] >> e[i];
}
FOR(i, 0, n)
{
FOR(j, 0, n)
{
if(i==j) continue;
if(s[j]<=e[i] && e[i]<e[j]){
ad[i].pb(j);
// cout << i << " -> " << j << '\n';
}
}
}
FOR(i, 0, n) FOR(j, 0, n) if(i!=j) d[i][j]=-1;
FOR(i, 0, n)
{
queue<int> q; q.push(i);
while(!q.empty()){
int u=q.front(); q.pop();
for(auto x : ad[u]){
if(d[i][x]==-1){
d[i][x]=d[i][u]+1;
q.push(x);
}
}
}
}
while(q--){
int a, b; cin >> a >> b; --a; --b;
if(d[a][b]==-1){
cout << "impossible\n";
}
else{
cout << d[a][b] << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
800 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |