|
4 | 4 |
|
5 | 5 | # This problem can be reduced to Independent Set problem (ISP) which is an NP problem
|
6 | 6 | # It's a special case of ISP
|
7 |
| -# It's can be solved effeciently with Dynamic Programming technique: |
8 |
| -# Let's W(v) reprensents the Total Fun (weight) in the party, then: |
9 |
| -# F(v) = Max(weight(v) + sum(weight(u) for all u grand-children of v), |
10 |
| -# sum(weight(u) for all u children of v)) |
11 |
| - |
12 |
| -class Vertex: |
13 |
| - def __init__(self, weight): |
14 |
| - self.weight = weight |
15 |
| - self.children = [] |
| 7 | +# It's can be solved effeciently with Greedy Algorithm technique: |
| 8 | +# Greedy Choice: Select all leaves of the tree and Remove all their parents + Iterate |
| 9 | +# This choice is safe: |
| 10 | +# since the number of leaves >= the leaves' parents |
| 11 | +# This choice guarantees to get an IS longer or equal to the IS that we would have got if we selected the parents |
16 | 12 |
|
17 | 13 | class Tree:
|
18 |
| - def __init__(self, size, weights): |
| 14 | + def __init__(self, size, edges): |
19 | 15 | self.size = size
|
20 |
| - self.weights = [weights[i] for i in range(size)] |
21 |
| - |
22 |
| - |
23 |
| -def ReadTree(): |
24 |
| - |
25 |
| - |
26 |
| - |
27 |
| -def dfs(tree, vertex, parent): |
28 |
| - for child in tree[vertex].children: |
29 |
| - if child != parent: |
30 |
| - dfs(tree, child, vertex) |
31 |
| - |
32 |
| - # This is a template function for processing a tree using depth-first search. |
33 |
| - # Write your code here. |
34 |
| - # You may need to add more parameters to this function for child processing. |
| 16 | + self.build_children_list(edges) |
35 | 17 |
|
| 18 | + def build_children_list(self, edges): |
| 19 | + self.children_list = [[] for _ in range(self.size)] |
| 20 | + for p, c in edges: |
| 21 | + self.children_list[p - 1].append(c - 1) |
| 22 | + self.children_list[c - 1].append(p - 1) |
36 | 23 |
|
37 |
| -def MaxWeightIndependentTreeSubset(tree): |
38 |
| - size = len(tree) |
39 |
| - if size == 0: |
40 |
| - return 0 |
41 |
| - dfs(tree, 0, -1) |
42 |
| - # You must decide what to return. |
43 |
| - return 0 |
44 |
| - |
| 24 | + def isp_dfs(self, v, parent): |
45 | 25 |
|
| 26 | + list = [] |
| 27 | + return list |
| 28 | + |
46 | 29 | def main():
|
47 |
| - tree = ReadTree() |
48 |
| - weight = MaxWeightIndependentTreeSubset(tree) |
49 |
| - print(weight) |
| 30 | + |
| 31 | + size = int(input()) |
| 32 | + edges = [] |
| 33 | + for _ in range(1, size): |
| 34 | + edges.append(list(map(int, input().split()))) |
| 35 | + tree = Tree(size, edges) |
| 36 | + |
| 37 | + isp_list = tree.isp_dfs(0, -1) |
| 38 | + print(len(isp_list)) |
| 39 | + print(isp_list) |
50 | 40 |
|
51 | 41 | if __name__ == "__main__":
|
52 | 42 |
|
53 | 43 | # To avoid stack overflow issues
|
54 | 44 | sys.setrecursionlimit(10**6) # max depth of recursion
|
55 | 45 | threading.stack_size(2**26) # new thread will get stack of such size
|
56 | 46 |
|
57 |
| - size = int(input()) |
58 |
| - weights = map(int, input().split()) |
59 |
| - print(weights) |
60 |
| - #tree = [Vertex(w) for w in map(int, input().split())] |
61 |
| - #for i in range(1, size): |
62 |
| - # a, b = list(map(int, input().split())) |
63 |
| - # tree[a - 1].children.append(b - 1) |
64 |
| - # tree[b - 1].children.append(a - 1) |
65 |
| - #return tree |
66 |
| - |
67 | 47 | # This is to avoid stack overflow issues
|
68 |
| - #threading.Thread(target=main).start() |
| 48 | + threading.Thread(target=main).start() |
0 commit comments