Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- STAGE
- Restore
- boostcamp #aI tech #주간리뷰
- git commit
- 함수
- github
- 파이썬
- commit
- amend
- 15596
- git
- python
- Push
- 수정사항업데이트
- 1
- regExr
- 1330
- local repository 생성
- git config global
- Baekjoon
- ADD
- remote repository 생성
- git push
- 두수비교하기
- 정규식
- 백준
- GitHub 설치
- Reset
Archives
- Today
- Total
Très bien
Baekjoon 2606 - 바이러스 (Python) 본문
Virus
◎ Problem Definition
2606번: 바이러스
첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어
www.acmicpc.net
◎ Implementation
1. Problem Type
Graph, DFS (Silver 3)
2. Problem Analysis
1번 컴퓨터를 통해 웜 바이러스에 걸리게 되는 컴퓨터의 수를 출력하는 프로그램.
- 연결된 노드의 수를 구해야 하기 때문에 visited는 bool 이 아닌 int로 적용했다.
- 방문 노드를 확인하는 stack을 구성했다.
3. Solution_1 (☆초기 접근)
▶ 완성 코드
# bj_2606_Virus
# Feb. 25th, 2022
# by Elie_H
# input : N
n = int(input()) # num_node, N < 100
visited = [0] * (n+1)
m = int(input()) # num_edge
edges = []
for i in range(m):
edges.append(list(map(int, input().split())))
# group 내 원소가 없을 때까지 edges를 돌며 visited = 0 인 항목을 추가합니다.
# 첫번째 값 '1' add
visited[1] = 1
group = [1]
while len(group) != 0 :
edge = group.pop(0)
for e1, e2 in edges:
if e1 == edge and visited[e2] == 0:
visited[e2] = 1
group.append(e2)
continue
elif e2 == edge and visited[e1] == 0:
visited[e1] = 1
group.append(e1)
continue
# print(visited)
print(sum(visited)-1)
▶ 성능
- memory : 30864 KB
- time : 72 msec
◎ Results
- pop() 함수를 사용하여 간단히 구현했다.
- BFS처럼 재귀적 호출 구조를 적용하면 수행시간이 조금 더 빨라질 것 같다. (시간나면 다시 해보자!!!)
'Coding > Algorithm' 카테고리의 다른 글
1로 만들기 (0) | 2022.07.22 |
---|---|
프린터 큐 (0) | 2022.06.25 |
Baekjoon 9020 - 골드바흐의 추측 (Python) (0) | 2022.02.21 |
BAEKJOON 11653 - 소인수분해 (0) | 2022.02.20 |
BAEKJOON 1978 - 소수 찾기 (0) | 2022.02.17 |
Comments