Advertisement
mynameishandsome

Untitled

Jun 29th, 2021
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 5001;
  4. bool anhyeuem[N][N] = {false};
  5. bool sua[N] = {false};
  6. bool ketqua =  false;
  7.  
  8.  
  9. bool dfs(int u, int v, int n){
  10.     sua[u] = true;
  11.     if (u == v){
  12.         ketqua = true;
  13.     }
  14.     else{
  15.         for (int i; i != n+1; i++){
  16.             if (ketqua != false) break;
  17.             if(sua[i] == false && anhyeuem[u][i] == true){
  18.                 dfs(i,v,n);
  19.             }
  20.         }
  21.     }
  22. }
  23.  
  24.  
  25. bool findPath(int n, std::vector<std::vector<int>> edges, int u, int v){
  26.     for(int i = 0; i!= edges.size();i++){
  27.         anhyeuem[edges[i][0]][edges[i][1]] = true;
  28.     }
  29.     dfs(u,v,n);
  30.     return ketqua;
  31. }
  32.  
  33. int main(){
  34.     cout << findPath(5,{{1,2},{2,3},{1,3},{4,5},{5,4}}, 1,4);
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement