1 / 4
Oct 2021

Unable to get the testcase for which the below code is failing. Request some hints

from collections import defaultdict

class Graph:

   # Constructor
   def __init__(self):

       # default dictionary to store graph
       self.graph = defaultdict(list)

   # function to add an edge to graph
   def addEdge(self,u,v):
       self.graph[u].append(v)

   
   def BFS(self):
       try:
           # Mark all the vertices as not visited
           visited = [False] * len(self.graph)
           color = [-1] * len(self.graph)

           # Create a queue for BFS
           queue = []
   
           for s in range(len(self.graph)):            
               
               # Mark the source node as
               # visited and enqueue it
               if visited[s] == False:
                   queue.append(s)
                   visited[s] = True
                   color[s] = 0
                   while queue:
       
                       # Dequeue a vertex from
                       # queue and print it
                       s = queue.pop(0)
                       #print (int(s)+1, end = " ")
           
                       # Get all adjacent vertices of the
                       # dequeued vertex s. If a adjacent
                       # has not been visited, then mark it
                       # visited and enqueue it
                       for i in self.graph[s]:
                           if visited[i] == False:
                               queue.append(i)
                               visited[i] = True
                               color[i] = 1-color[s]
                           elif i != s:
                               if color[i] == color[s]:
                                   return False
           return True
       except:
           pass
           


iTc = int(input().strip())
iSc = 1
while iTc !=0:
   lstNE = [int(i) for i in input().strip().split(" ")]
   iNodes = lstNE[0]
   iEdges = lstNE[1]
   g = Graph()

   for i in range(iEdges):
       lstNodes = [int(i) for i in input().strip().split(" ")]
       
       g.addEdge(int(lstNodes[0])-1, int(lstNodes[1])-1)
       g.addEdge(int(lstNodes[1])-1, int(lstNodes[0])-1)
       
   print("Scenario #"+str(iSc)+":")
   if g.BFS() == False:
       print("Suspicious bugs found!")
   else:
       print("No suspicious bugs found!")

   iTc = iTc - 1
   iSc = iSc + 1
'''```
  • created

    Oct '21
  • last reply

    Oct '21
  • 3

    replies

  • 740

    views

  • 2

    users

  • 1

    link

14 days later

Indentation is important right? Please edit your post and put three back ticks ``` on a line by themselves both before and after the code to preserve the indentation and code formatting.

I tried it on IDEONE, but got the error

Traceback (most recent call last):
File “./prog.py”, line 62, in
File “./prog.py”, line 7, in init
NameError: name ‘defaultdict’ is not defined

BUGLIFE3