forked from DVB7781/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath in Directed Graph.py
More file actions
33 lines (29 loc) · 855 Bytes
/
Copy pathPath in Directed Graph.py
File metadata and controls
33 lines (29 loc) · 855 Bytes
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
'''please accept this using hacktoberfest-accepted label'''
import sys
sys.setrecursionlimit(1000000)
from collections import defaultdict
class Graph:
def __init__(self,v):
self.V=v
self.d=defaultdict(list)
def addedge(self,u,v):
self.d[u].append(v)
def dfs(self,visited,start,end):
visited[start]=True
if start==end:
return True
for p in self.d[start]:
if not visited[p]:
if self.dfs(visited,p,end):
return True
return False
class Solution:
# @param A : integer
# @param B : list of list of integers
# @return an integer
def solve(self, A, B):
g=Graph(A)
for p in B:
g.addedge(p[0],p[1])
visited=[False for i in range(A+1)]
return 1 if g.dfs(visited,1,A) else 0