-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from seungyeonnnnnni/main
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from collections import deque | ||
def main(): | ||
n = 6 | ||
vertex=[[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] | ||
print(solution(n,vertex)) | ||
|
||
def solution(n,edge): | ||
answer = 0 | ||
|
||
adj = [[] for i in range(n+1)] | ||
visited = [0]*(n+1) | ||
|
||
# 인접 행렬 만들기 | ||
for i,j in edge: | ||
adj[i].append(j) | ||
adj[j].append(i) | ||
|
||
# 초기값 | ||
q = deque([1]) | ||
visited[1] = 1 | ||
|
||
bfs(1,adj,visited) | ||
|
||
for i in visited: | ||
if i == max(visited): | ||
answer += 1 | ||
return answer | ||
|
||
def bfs(start,adj,visited): # start 에서 시작해 갈 수 있는 모든 정점 | ||
que = deque([start]) | ||
visited[1] = 1 | ||
|
||
while que: | ||
v = que.popleft() | ||
for i in adj[v]: | ||
if not visited[i]: | ||
visited[i] = visited[v] + 1 | ||
que.append(i) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
if __name__ == "__main__": | ||
main() |