2018 Google kickstart Problem A. Planet Distance
生活随笔
收集整理的這篇文章主要介紹了
2018 Google kickstart Problem A. Planet Distance
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
Small dataset
3 ≤ N ≤ 30.
Large dataset
3 ≤ N ≤ 1000.input
2
5
1 2
2 3
3 4
2 4
5 3
3
1 2
3 2
1 3Output
Case #1: 1 0 0 0 1
Case #2: 0 0 0In Sample Case #1, the cycle consists of planets 2, 3, and 4. Therefore, the distances for planets 2, 3, and 4 are 0. There is a vacuum tube between 1 and 2, and another vacuum tube between 3 and 5. Thus, planets 1 and 5 are at a distance 1 from the cycle.In Sample Case #2, all the planets are part of the cycle. Hence, their distances are 0.
題目大致意思是,有很多星球,在每個星期之間會有管道,當這些管道和星球形成環的時候,這些形成環的星球上會有一些禮物,這些形成環的星球離禮物的距離為0,其它星球離禮物的距離,是到形成環的所有星球中距離最短的那條,求所有星球到禮物的距離。
解題思路
這題主要是找到有環無向圖上的所有節點距離環的最短距離, 可以先用DFS找到環上的點,然后去BFS去求每個節點到環上的距離。
代碼:
# -*- coding: utf-8 -*-# !/usr/bin/env python# Time: 2018/5/27 13:34# Author: sty# File: A Planet Distance.pyfrom collections import defaultdict# This class represents a undirected graph using adjacency list representation
class Graph:def __init__(self, vertices, dis, res):self.V = vertices # No. of verticesself.distance = disself.res = resself.graph = defaultdict(list) # default dictionary to store graph# function to add an edge to graphdef addEdge(self, v, w):self.graph[v].append(w) # Add w to v_s listself.graph[w].append(v) # Add v to w_s list# A recursive function that uses visited[] and parent to detect# cycle in subgraph reachable from vertex v.def isCyclicUtil(self, v, visited, parent):# Mark the current node as visitedvisited[v] = True# Recur for all the vertices adjacent to this vertexfor i in self.graph[v]:# If the node is not visited then recurse on itif visited[i] == False:if (self.isCyclicUtil(i, visited, v)):return True# If an adjacent vertex is visited and not parent of current vertex,# then there is a cycleelif parent != i:self.res.append(v)return Truereturn False# Returns true if the graph contains a cycle, else false.def isCyclic(self):# Mark all the vertices as not visitedvisited = [False] * (self.V + 1)# Call the recursive helper function to detect cycle in different# DFS treesfor i in range(1, self.V + 1):if visited[i] == False: # Don't recur for u if it is already visitedif (self.isCyclicUtil(i, visited, -1)) == True:return Truereturn Falsedef judge(self, u, v):visited = [False] * (self.V + 1)distance = [0 for i in range(self.V + 1)]queue = []queue.append(u)visited[u] = Truewhile queue:x = queue.pop(0)for i in self.graph[x]:if visited[i] == False:distance[i] = distance[x] + 1queue.append(i)visited[i] = Truereturn distance[v]def count_dis(self):cycle_list = []for i in self.graph[self.res[0]]:cycle_list.append(i)cycle_list.append(self.res[0])visited = [False] * (self.V + 1)for i in range(1, self.V + 1):if i in cycle_list:self.distance[i] = 0else:len_min = self.V + 1for j in cycle_list:tem_min = self.judge(i, j)if tem_min < len_min:len_min = tem_minself.distance[i] = len_minif __name__ == '__main__':# input() reads a string with a line of input, stripping the '\n' (newline) at the end.# This is all you need for most Kickstart problems.t = int(input()) # read a line with a single integerfor ti in range(1, t + 1):n = int(input())dis = [0 for i in range(n + 1)]res = []g = Graph(n, dis, res)for i in range(n):n, m = [int(s) for s in input().split(" ")]g.addEdge(n, m)if g.isCyclic():g.count_dis()print("Case #{0}:".format(ti), end='')for x in dis[1:]:print(' ',x, end='')print('\n', end='')# check out .format's specification for more formatting options
輸入文件在這里,大家感興趣可以測試下
后記
這是Google 2018的kickstart在線測試題,如果不知道這個測試題的同學可以自行百度,這次總共三道題,很可惜,我只做了第一道題,而且最后測試,雖然我感覺沒什么問題,但是提示我錯誤了。總體感覺Google的kickstart題目考察范圍十分的有內涵,一道題里面可能有好幾個知識點,但是也看見有大神不到半個小時就把三個題都做了的,總的來說還是自己太水。
隨便吐槽下kickstart的做題方式是要自己將輸入測試集下載下來,然后提交輸出結果集和程序上去,判斷錯誤,感覺這樣的過程很是麻煩。
總結
以上是生活随笔為你收集整理的2018 Google kickstart Problem A. Planet Distance的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 语音识别(ASR)评估指标-WER(字错
- 下一篇: linux环境下快速配置hadoop集群