Submission
Status:
(PPPP)(PPPPPP)(PPPPPPPPPP)
Subtask/Task Score:
{30/30}{50/50}{20/20}
Score: 100
User: anonymous
Problemset: ย้อนศร
Language: cpp
Time: 0.322 second
Submitted On: 2026-10-18 15:02:24
#include <bits/stdc++.h>
using namespace std;
const int k = 1e9;
vector<pair<int , int>> graph[100005];
int dist[100005],a,b,n,m,st,en,di,no,nd;
deque<pair<int , int>> dq;
int solution() {
for(int i=1;i<=n;i++) dist[i] = k;
dq.push_front({0 , st});
dist[st] = 0;
while(dq.size()){
di = dq.front().first;
no = dq.front().second;
dq.pop_front();
if(di > dist[no])continue;
for(auto i : graph[no]){
nd = di + i.first;
if(nd < dist[i.second]) {
dist[i.second] = nd;
if(i.first == 0) dq.push_front({di + i.first , i.second});
else dq.push_back({di + i.first , i.second});
}
}
}
return dist[en];
}
int main(){
cin >> n >> m;
while(m--) {
cin >> a >> b;
graph[a].push_back({0, b});
graph[b].push_back({1, a});
}
int q; cin >> q;
while(q--){
cin >> st >> en;
cout << solution() << '\n';
}
return 0;
}
/*output :
11 13
8 9
1 2
3 6
6 7
11 10
2 9
1 3
8 11
10 9
2 4
4 5
6 3
10 8
3
7 11
10 1
1 5
*/