Skip to content

Commit

Permalink
Add prayatna solution
Browse files Browse the repository at this point in the history
  • Loading branch information
thorvn committed Oct 2, 2022
1 parent d426dc9 commit d053dc3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Lecture-06-DFS/prayatna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

MAX = 100005
graph = [[] for _ in range(MAX)]
visited = [False] * MAX

# Adjacency List
def read_graph(m):
for _ in range(m):
node_x, node_y = map(int, input().split())
graph[node_x].append(node_y)
graph[node_y].append(node_x)

def dfs(root):
stack = []

visited[root] = True
stack.append(root)

while len(stack) > 0:
node = stack.pop()
for node_realated in graph[node]:
if not visited[node_realated]:
visited[node_realated] = True
stack.append(node_realated)

t = int(input())
for _ in range(t):
n = int(input())
# Reset
graph = [[] for _ in range(MAX)]
visited = [False] * MAX

e = int(input())
read_graph(e)

count = 0
for i in range(n):
if visited[i] == False:
dfs(i)
count += 1
print(count)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Solution for Big-O Blue course
## Lecture 6: DFS

- Bishu and his Girlfriend (from: [Hackerrank](https://assessment.hackerearth.com/challenges/college/nits-local-10/algorithm/84888e824aa04fc793c3beefca02b5a7/))
- Prayatna (from: [Spoj](https://vn.spoj.com/problems/CAM5/))

0 comments on commit d053dc3

Please sign in to comment.