日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

2018 Google kickstart Problem A. Planet Distance

發(fā)布時(shí)間:2023/11/28 生活经验 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2018 Google kickstart Problem A. Planet Distance 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

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.

題目大致意思是,有很多星球,在每個(gè)星期之間會(huì)有管道,當(dāng)這些管道和星球形成環(huán)的時(shí)候,這些形成環(huán)的星球上會(huì)有一些禮物,這些形成環(huán)的星球離禮物的距離為0,其它星球離禮物的距離,是到形成環(huán)的所有星球中距離最短的那條,求所有星球到禮物的距離。

解題思路

這題主要是找到有環(huán)無(wú)向圖上的所有節(jié)點(diǎn)距離環(huán)的最短距離, 可以先用DFS找到環(huán)上的點(diǎn),然后去BFS去求每個(gè)節(jié)點(diǎn)到環(huán)上的距離。

代碼:

# -*- 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

輸入文件在這里,大家感興趣可以測(cè)試下

后記

這是Google 2018的kickstart在線測(cè)試題,如果不知道這個(gè)測(cè)試題的同學(xué)可以自行百度,這次總共三道題,很可惜,我只做了第一道題,而且最后測(cè)試,雖然我感覺(jué)沒(méi)什么問(wèn)題,但是提示我錯(cuò)誤了。總體感覺(jué)Google的kickstart題目考察范圍十分的有內(nèi)涵,一道題里面可能有好幾個(gè)知識(shí)點(diǎn),但是也看見(jiàn)有大神不到半個(gè)小時(shí)就把三個(gè)題都做了的,總的來(lái)說(shuō)還是自己太水。
隨便吐槽下kickstart的做題方式是要自己將輸入測(cè)試集下載下來(lái),然后提交輸出結(jié)果集和程序上去,判斷錯(cuò)誤,感覺(jué)這樣的過(guò)程很是麻煩。

總結(jié)

以上是生活随笔為你收集整理的2018 Google kickstart Problem A. Planet Distance的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。