Skip to content

Commit a4829d5

Browse files
author
Hamid Gasmi
committed
Issue #153 is started
1 parent 66a75ff commit a4829d5

File tree

9 files changed

+52
-22
lines changed

9 files changed

+52
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
#uses python3
2-
32
import sys
43
import threading
54

6-
# This code is used to avoid stack overflow issues
7-
sys.setrecursionlimit(10**6) # max depth of recursion
8-
threading.stack_size(2**26) # new thread will get stack of such size
9-
5+
# This problem can be reduced to Independent Set problem (ISP) which is an NP problem
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))
1011

1112
class Vertex:
1213
def __init__(self, weight):
1314
self.weight = weight
1415
self.children = []
1516

17+
class Tree:
18+
def __init__(self, size, weights):
19+
self.size = size
20+
self.weights = [weights[i] for i in range(size)]
1621

22+
1723
def ReadTree():
18-
size = int(input())
19-
tree = [Vertex(w) for w in map(int, input().split())]
20-
for i in range(1, size):
21-
a, b = list(map(int, input().split()))
22-
tree[a - 1].children.append(b - 1)
23-
tree[b - 1].children.append(a - 1)
24-
return tree
25-
24+
25+
2626

2727
def dfs(tree, vertex, parent):
2828
for child in tree[vertex].children:
@@ -44,10 +44,25 @@ def MaxWeightIndependentTreeSubset(tree):
4444

4545

4646
def main():
47-
tree = ReadTree();
48-
weight = MaxWeightIndependentTreeSubset(tree);
47+
tree = ReadTree()
48+
weight = MaxWeightIndependentTreeSubset(tree)
4949
print(weight)
5050

51+
if __name__ == "__main__":
52+
53+
# To avoid stack overflow issues
54+
sys.setrecursionlimit(10**6) # max depth of recursion
55+
threading.stack_size(2**26) # new thread will get stack of such size
56+
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
5166

52-
# This is to avoid stack overflow issues
53-
threading.Thread(target=main).start()
67+
# This is to avoid stack overflow issues
68+
#threading.Thread(target=main).start()
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
1
2-
1000
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
1000
1+
1
2+
1
3+
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
2
22
1 2
3-
1 2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
1
12
2
3+

4-np-complete-problems/2-coping_with_np_completeness/plan_party_tests/03

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
5
2-
1 5 3 7 5
32
5 4
43
2 3
54
4 2
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
11
1+
3
2+
1 3 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
11
2+
1 2
3+
1 3
4+
1 4
5+
2 5
6+
3 6
7+
4 7
8+
4 8
9+
7 9
10+
7 10
11+
7 11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
7
2+
1 5 6 8 9 10 11

0 commit comments

Comments
 (0)