Copy CodeThe code is as follows:
#_ *_coding:utf_8_
Import Sys
Import OS
Class Graph ():
def __init__ (self, V, E):
Self. v = V
Self. E = E
self.visited = []
Self.dict = {}
SELF.FD = open ("Input.txt")
def initgraph (self):
self.visited = [0 for I in range (self. V+1)]
For I in range (self. E):
F, t = map (int, Self.fd.readline (). Split ())
#f, t = map (int, Sys.stdin.readline (). Split ())
If Self.dict.has_key (f) ==false:
L = []
L.append (t)
SELF.DICT[F] = L
Else
L = self.dict[f]
L.append (t)
SELF.DICT[F] = L
def dfsgraph (self, src):
SELF.VISITED[SRC] = 1
Print SRC,
If Self.dict.get (SRC): An exception occurs #self. dict[src]
For u in SELF.DICT[SRC]:
If self.visited[u]==0:
Self.dfsgraph (U)
Graph = Graph (6, 10)
Graph.initgraph ()
Graph.dfsgraph (1)
Nput.txt
Copy the Code code as follows:
1 2
1 3
1 4
3 2
2 6
4 3
3 5
4 5
6 5
3 6
Output
Copy the Code code as follows:
1 2 6 5 3 4