高级农民
 
- 积分
- 3542
- 学分
- 个
- 大米
- 升
- 人参
- 枚
- 水井
- 尺
- 小麦
- 颗
- 萝卜
- 根
- 小米
- 粒
- UID
- 63317
- 注册时间
- 2012-6-20
- 最后登录
- 1970-1-1
- 在线时间
- 小时
- 好友
- 收听
- 听众
- 日志
- 相册
- 帖子
- 主题
- 分享
- 精华
|
- x = [('parker', 'parker'), ('laks', 'parker'), ('avinash', 'laks'), ('jonathan', 'avinash'), ('jason', 'laks'), ('david', 'parker'), ('arisa', 'david') ]. 1point 3acres 璁哄潧
- #def buildGraph(inputs):
- #for element in x:
- # print element[1]
- def buildGraph(x):
- graph = {}
- for element in x:
- if element[0] == element[1]:
- graph['boss'] = [element[0]]. 1point3acres.com/bbs
- elif graph.get(element[1], []):
- graph[element[1]].append(element[0])
- else:
- graph[element[1]] = [element[0]]
- return graph
- graph = buildGraph(x)
- def dfs(graph):
- stack = []
- visited = set()
- for boss in graph['boss']: 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
- stack.append([0, boss])
- #visited.add(boss)
- while stack:
- count, boss = stack.pop()
- if boss not in visited:. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
- visited.add(boss)
- print ' '*count, boss. Waral 鍗氬鏈夋洿澶氭枃绔,
- if graph.get(boss, None):. From 1point 3acres bbs
- for employee in graph[boss]:
- if employee not in visited:
- stack.append([count+1, employee])
- #print visited
- dfs(graph)
复制代码 |
|