fork(1) download
  1. def calcPathLen ( initNode , lastNode ) :
  2. pathWeight = 0
  3. if initNode == lastNode :
  4. return 0
  5. else :
  6. for nextNodeOption, edgeNum in treeMapDict[initNode] :
  7. return edgeDict[edgeNum][-1] + calcPathLen(nextNodeOption,lastNode)
  8.  
  9. ################## calcPathLen end ######################################
  10.  
  11.  
  12. def updateIthWeight ( edgeToUpdate, newWeight) :
  13. edgeDict[edgeToUpdate][-1] = newWeight
  14.  
  15. ################## updateIthWeight end ######################################
  16.  
  17. def getNodesData (num) :
  18. for i in range(1,int(num)) :
  19. data = raw_input()
  20. dataLst = data.split()
  21. edgeDict[i] = [(int(dataLst[0]),int(dataLst[1])),int(dataLst[2])]
  22. if int(dataLst[0]) in treeMapDict :
  23. treeMapDict[int(dataLst[0])].append((int(dataLst[1]),i))
  24. else:
  25. treeMapDict[int(dataLst[0])] = [(int(dataLst[1]),i)]
  26.  
  27. ################## getNodesData end ######################################
  28.  
  29. def getQueriesData (queriesNum) :
  30. for i in range(0,int(queriesNum)) :
  31. qLst.append(raw_input())
  32.  
  33. ################## getQueriesData end ######################################
  34.  
  35. ###################
  36. ## get nodes data
  37. ###################
  38. edgeDict = dict()
  39. treeMapDict = dict()
  40. numOfNodes = raw_input()
  41. getNodesData(numOfNodes)
  42.  
  43. ###################
  44. ##get queries data
  45. ###################
  46. qLst = []
  47. queriesNum = raw_input()
  48. getQueriesData(queriesNum)
  49.  
  50. ###################
  51. # run queries
  52. ###################
  53.  
  54. for q in qLst:
  55. querySpltLst = q.split()
  56. if querySpltLst[0] == '1' :
  57. edgeToUpdate = int(querySpltLst[1])
  58. newWeight = int(querySpltLst[2])
  59. updateIthWeight(edgeToUpdate,newWeight)
  60. elif querySpltLst[0] == '2' :
  61. initNode = int(querySpltLst[1])
  62. lastNode = int(querySpltLst[2])
  63. print calcPathLen(initNode,lastNode)
  64.  
Runtime error #stdin #stdout #stderr 0.01s 7900KB
stdin
3
1 2 1
2 3 2
6
2 1 2
2 2 1
2 1 3
2 3 1
2 2 3
2 3 2
stdout
1
stderr
Traceback (most recent call last):
  File "prog.py", line 63, in <module>
  File "prog.py", line 7, in calcPathLen
  File "prog.py", line 6, in calcPathLen
KeyError: 3