incorrect answer on two tests of the problem about the size of the connectivity component in the graph. Search in depth

Task condition:

An undirected unweighted graph is given. For it, you need to find the number of vertices that lie in the same connectivity component with a given vertex (counting this vertex).

The graph is represented as an adjacency matrix.
Immediately wrote a solution through a deep search:

def dfs(v):
    visited.append(v)

    for i in range(n):
        if graph[v][i] == 1 and i not in visited:
            dfs(i)


n, s = map(int, input().split())
graph = [list(map(int, input().split())) for x in range(n)]

visited = []
dfs(s)

print(len(visited))
  1. Starting the function dfs at the starting vertex
  2. Adding it to the visited list
  3. We run through each one top
  4. Check whether it is connected to the current one and whether it is in the visited list
  5. If everything is OK, then run dfs
  6. At the end, we count the number of elements in the list visited and output it

The program gives an incorrect answer on two tests. Link to the checking system


CHADNT?

(I've seen other implementations, but I wanted to find out what exactly is wrong with my code)

Author: jfs, 2017-12-03

1 answers

The number of the given vertex s is set in the interval 1 ≤ s ≤ n, i.e. the numbering from one.

(An error can be detected, for example, on a graph from a single vertex)

 3
Author: Pavel, 2017-12-12 08:28:36