Skip to content

Instantly share code, notes, and snippets.

@jo32
Last active October 20, 2015 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jo32/3f67245defcec6f62ae5 to your computer and use it in GitHub Desktop.
Save jo32/3f67245defcec6f62ae5 to your computer and use it in GitHub Desktop.
CODEFORCE 120F

cpp 版显示 wrong answer,没有输出, python 版显示 runtime error,没有提示,但是本地测试过测试用例都没有问题,算法应该是正确的。

cases = int(raw_input())
sumLength = 0
def maxHeight(edgeDict, root, visited=set([])):
visited.add(root);
return max([maxHeight(edgeDict, child, visited) if child not in visited else -1 for child in edgeDict[root]]) + 1
for case in range(cases):
edgeDict = {}
line = [int(i) for i in raw_input().split(' ')]
ni = line[0];
for i in range(ni - 1):
u = line[i * 2 + 1]
v = line[i * 2 + 2]
if u in edgeDict:
edgeDict[u].append(v)
else:
edgeDict[u] = [v]
if v in edgeDict:
edgeDict[v].append(u)
else:
edgeDict[v] = [u]
sumLength += max([maxHeight(edgeDict, i, set([])) for i in range(1, ni + 1)])
print sumLength
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment