how to know if two vertexes are connected in graph c++
#include <bits/stdc++.h>
using namespace std;
// We can use dfs alrogithm.
// in this example, we have an vector 'graph' in which indexes are connected 
// to each other, and an index of vertices we want to check if they are connected
// lets denote them as 'x' and 'y'
//NOTE THAT THIS ONLY WORKS ON NON-CYCLIC GRAPHS
bool dfs(int currIndx,int wantedIndx,vector<vector<int>> graph){
  	if(currIndx == wantedIndx)return true;
  	for(auto it : graph[currIndx]){
      if(dfs(it,wantedIndx,graph)) return true;
    }
  	return false;
}
int main(){
  // Create connections
  vector<int> Zero = {1,3};
  vector<int> One = {2};
  vector<int> Two = {4};
  vector<int> Three = {1};
  vector<int> Four = {0};
  // now add them all to one graph
  vector<vector<int>> graph(5);
  graph[0] = Zero;
  graph[1] = One;
  graph[2] = Two;
  graph[3] = Three;
  graph[4] = Four;
  // For this example lets check if 0 and 4 are somehow connected to each other
  bool answer = dfs(0,4,graph);
  if(answer == true)cout << "Yes, They Are Connected" << endl;
  else cout << "NO, They Are Not Connected ;(" << endl;
}

C++相关代码片段

how to kill

hello world cpp

cpp hello world

when kotlin

how to write hello world c++

fenwick tree

main function

python loop

is palindrom

subsequence

insertion sort

react cookie

data structure

Stack Modified

increment integer

496. Next Greater Element I.cpp

c++ freecodecamp course 10 hours youtube

simple interest rate

deliberation meaning

fingerprint

c++ system() from variable

operator using

unambiguous

Stream Overloading

quick_sort

hash table

graph colouring

the question for me

fname from FString

how togreper

is plaindrome

logisch oder

robot framework for loop example

spyder enviroment

pallindrome string

ue4 c++ string from fvector

interactive problem

two sum solution

interpolation search

Required Length

new expression

Targon lol

xor linked list

stack implementation

typescript

loop through array

loop array

how to point to next array using pointer c++

file exist

floating point exception

array di struct

castin in C++

varint index

how to make a instagram report bot python

windows servis from console app

add integers

listas enlazadas/ linked lists

niet werkend

bubble sort

triangle angle sum

divisor summation

rotateArray

destiny child

Populating Next Right Pointers in Each Node

cosnt cast

bucket sort

double plus overload

Z-function

binary search

permutation

linked list

Implicit conversion casting

square root

public method

tower of hanoi

selection sort

dangling pointer

hello world programming

statements

volumeof a sphere