-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path가장 먼 노드.py
87 lines (63 loc) · 1.85 KB
/
가장 먼 노드.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
#2020.03.08
from collections import defaultdict, deque
def solution(n, edge):
val = {i:0 for i in range(1,n+1)}
edges = defaultdict(list)
for a, b in edge:
edges[a].append(b)
edges[b].append(a)
queue = deque(edges[1])
cnt = 1
while queue:
for i in range(len(queue)):
current = queue.popleft()
if val[current] == 0:
val[current] = cnt
for num in edges[current]:
queue.append(num)
cnt += 1
del val[1]
maxx = max(val.values())
answer = 0
for i in val.values():
if i == maxx:
answer += 1
return answer
"""
'''
def solution(n, edge):
diction = {i:[] for i in range(1,n+1)}
for a,b in edge:
diction[a].append(b)
diction[b].append(a)
start = set([1])
visited = set(start)
next = set([])
while True:
for num in start:
next.update(set(diction[num]))
next = next - visited
visited = visited | next
if not next:
return len(start)
start = next.copy()
'''
from collections import deque
def solution(n, vertex):
visited = [1]
queue = deque([[1,1]])
answer = []
while queue:
start_node, cost = queue.popleft()
answer.append([start_node, cost])
for node1, node2 in vertex:
if node1 in visited and node2 in visited:
continue
if start_node in [node1, node2]:
new_node = node1+node2-start_node
queue.append([new_node, cost+1])
visited.append(new_node)
max_cost = answer[-1][-1]
answer = [node for node, cost in answer if cost == max_cost]
return len(answer)