2018-08-25 字节跳动笔试5-已知一些形如“y=4-x”的约束关系,查询形如“y-x”的值

image.png
image.png

木有验证结果,样例可以正确输出。

class Graph(object):

    def __init__(self,*args,**kwargs):
        self.node_neighbors = {}
        self.visited = {}
        self.edgeWeight = {}

    def add_nodes(self,nodelist):

        for node in nodelist:
            self.add_node(node)

    def add_node(self,node):
        if not node in self.nodes():
            self.node_neighbors[node] = []

    def add_edge(self,edge,weight):
        u,v = edge
        if(v not in self.node_neighbors[u]) and ( u not in self.node_neighbors[v]):
            self.node_neighbors[u].append(v)
            self.edgeWeight[edge]=weight

            if(u!=v):
                self.node_neighbors[v].append(u)


    def get_weight(self,edge):
        return self.edgeWeight[edge]

    def nodes(self):
        return self.node_neighbors.keys()

    def depth_first_search(self,root=None, goal=None):
        self.visited = {}
        order = []
        def dfs(node):
            flag = False
            self.visited[node] = True
            order.append(node)
            if node == goal:
                return True

            for n in self.node_neighbors[node]:
                if not n in self.visited:
                    flag = dfs(n)
            if flag:
                return True
            else:
                order.remove(node)
                return False


        if root:
            dfs(root)

        # for node in self.nodes():
        #     if not node in self.visited:
        #         dfs(node)

        # print(order)
        return order


if __name__ == '__main__':
    g = Graph()

    mn = input()
    # print("mn: ",mn)

    m = int(mn.split(' ')[0])
    n = int(mn.split(' ')[1])

    varList = []
    for i in range(m):
        constraint = input()
        # print(constraint)
        element = constraint.split(' ')
        var1 = element[0]
        g.add_node(var1)
        var2 = element[4]
        g.add_node(var2)
        weight = int(element[2])
        g.add_edge((var1,var2), weight)

    # print("nodes:", g.nodes())

    for j in range(n):
        query = input()
        # print(query)
        element = query.split(' ')
        var1 = element[0]
        var2 = element[2]
        order = []
        order = g.depth_first_search(var1,var2)

        if (len(order)-1)%2 != 0:
            print("cannot_answer")
        else:
            weightList = []
            for i in range(len(order)-1):
                edge = (order[i],order[i+1])
                weightList.append(g.get_weight(edge))
            # print(weightList)

            result = 0
            for i in range(len(weightList)-2,-1,-1):
                if i == len(weightList)- 2:
                    result = int(weightList[i]) - int(weightList[i+1])
                else:
                    result = int(weightList[i]) - result
                # result = -1*result
            print(result)

一道题做了快2个小时,大彪也没做出别的题。看来字节跳动的题难度很大啊。T_T

你可能感兴趣的:(2018-08-25 字节跳动笔试5-已知一些形如“y=4-x”的约束关系,查询形如“y-x”的值)