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 <gtest/gtest.h>
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
using std::pair;
void explore(vector<vector<int>> &adj, vector<int> &visited, int x, int y) {
visited.push_back(x);
if (x == y)
return;
for (size_t i = 0; i < adj[x].size(); i++) {
if (std::find(visited.begin(), visited.end(), adj[x][i])
== visited.end())
explore(adj, visited, adj[x][i], y);
}
}
int reach(vector<vector<int> > &adj, int x, int y) {
vector<int> visited;
if (x == y)
return 1;
explore(adj, visited, x, y);
if (std::find(visited.begin(), visited.end(), y) != visited.end())
return 1;
return 0;
}
//TEST(Reachable, Reach1) {
// vector<vector<int>> adj = { { 1, 3 }, { }, { 1 }, { 2 } };
// ASSERT_EQ(reach(adj, 0, 3), 1);
//}
int main() {
size_t n, m;
std::cin >> n >> m;
vector<vector<int> > adj(n, vector<int>());
for (size_t i = 0; i < m; i++) {
int x, y;
std::cin >> x >> y;
adj[x - 1].push_back(y - 1);
adj[y - 1].push_back(x - 1);
}
int x, y;
std::cin >> x >> y;
std::cout << reach(adj, x - 1, y - 1);
}
|