fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <unordered_map>
  4. #include <unordered_set>
  5. using namespace std;
  6.  
  7. void dfs(unordered_map<int, vector<int>> followMap, unordered_set<int>& visited, int follower) {
  8. visited.insert(follower);
  9. for (const auto& follow : followMap[follower]) {
  10. if (visited.find(follow) == visited.end()) {
  11. visited.insert(follow);
  12. dfs(followMap, visited, follow);
  13. }
  14. }
  15. }
  16.  
  17. int main() {
  18. int total_mem = 0, mem, total_edge = 0, follower, following;
  19. unordered_map<int, vector<int>> followMap;
  20. cin >> total_mem;
  21. unordered_set<int> visited(total_mem);
  22. for (int i = 0; i < total_mem; i++) cin >> mem;
  23. cin >> total_edge;
  24. for (int i = 0; i < total_edge; i++) {
  25. cin >> follower >> following;
  26. if (followMap.find(follower) == followMap.end())
  27. followMap[follower] = vector<int>();
  28. followMap[follower].push_back(following);
  29. }
  30. cin >> follower >> following;
  31. dfs(followMap, visited, follower);
  32. int canReach = visited.find(following) != visited.end() ? 1 : 0;
  33. cout << canReach;
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5280KB
stdin
4
2
5
7
9
4
2 9
7 2
7 9
9 5
7
9
stdout
1