-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsolve.cpp
49 lines (47 loc) · 1.2 KB
/
solve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int T;
cin >> T;
int u, v, src, dst;
while (T--) {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
}
cin >> src >> dst;
vector<bool> visited(n, false);
visited[src] = true;
// maintain a queue of pairs <node, dist>, pop when dist > 6 until queue is empty
queue<pair<int, int>> q;
q.push({src, 0});
bool hasPath = false;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
if (p.first == dst) {
hasPath = true;
break;
}
if (p.second >= 6) {
continue;
}
for (const auto next : adj[p.first]) {
if (!visited[next]) {
visited[next] = true;
q.push({next, p.second + 1});
}
}
}
if (hasPath) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}