#include <bits/stdc++.h>
using namespace std;
map<int, vector<int>> g;
int n,q;
int bfs(int x, int cel){
queue<int> q;
q.push(x);
vector<int> tav;
tav.assign(n+1, -1);
tav[x]=0;
while (!q.empty()){
int v=q.front();
q.pop();
for (int edge:g[v]){
if (edge==cel){
tav[edge]=tav[v]+1;
return tav[edge];
}
tav[edge]=tav[v]+1;
q.push(edge);
}
}
return -1;
}
int main() {
cin>>n>>q;
vector<pair<int, int>> idopontok;
for (int i=0; i<n;i++){
int a,b;
cin>>a>>b;
g[a].push_back(b);
idopontok.push_back(make_pair(a,b));
}
for (int t=0; t<q;t++){
int x,y;
cin>>x>>y;
x--;
y--;
int cel=idopontok[y].second;
int mego=bfs(idopontok[x].second, cel);
if (mego!=-1){
cout<<mego<<endl;
}
else cout<<"IMPOSSIBLE"<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
123 ms |
25420 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |