-
Notifications
You must be signed in to change notification settings - Fork 0
/
Far Vertices
70 lines (58 loc) · 2.12 KB
/
Far Vertices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
def farVertices(n, k, edges):
tree = {}
for edge in edges:
tree[edge[0],edge[1]] = 1
tree[edge[1],edge[0]] = 1
tree_paths = len(tree)
cont_flag = True
while cont_flag:
for edge in edges:
matches = {x:y for x,y in tree.items() if edge[1] == x[0]}
for match in matches.keys():
if (edge[0],match[1]) not in tree.keys() and edge[0] != match[1]:
tree[edge[0],match[1]] = matches[match] + 1
tree[match[1],edge[0]] = matches[match] + 1
matches = {x:y for x,y in tree.items() if edge[0] == x[1]}
for match in matches.keys():
if (edge[1],match[0]) not in tree.keys() and edge[1] != match[0]:
tree[edge[1],match[0]] = matches[match] + 1
tree[match[0],edge[1]] = matches[match] + 1
if len(tree) == tree_paths:
cont_flag = False
tree_paths = len(tree)
removed = 0
cont_flag = True
while cont_flag:
matches = [x[0] for x,y in tree.items() if y>k]
if len(matches)==0:
cont_flag = False
else:
removed += 1
match_count = Counter(matches)
maxcount = max([y for x,y in match_count.items()])
match_max = [x for x,y in match_count.items() if y==maxcount]
remove_node = match_max[0]
nodes_to_remove = [x for x in tree.keys() if
x[0]==remove_node or x[1]==remove_node]
print(nodes_to_remove)
for node in nodes_to_remove:
del tree[node]
return removed
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
k = int(first_multiple_input[1])
edges = []
for _ in range(n - 1):
edges.append(list(map(int, input().rstrip().split())))
result = farVertices(n, k, edges)
fptr.write(str(result) + '\n')
fptr.close()