[백준] 1325번 효율적인 해킹 _ Python

2023. 11. 28. 22:40·Algorithms/Graph

https://www.acmicpc.net/problem/1325

 

 

[초기 접근 방법]

- '한 번에 가장 많은 컴퓨터를 해킹할 수 있는 컴퓨터 번호' 출력

- 각 노드, 즉 각 컴퓨터에서 bfs 탐색을 하며 그 깊이를 측정하고

   가장 깊은 노드를 출력하면 오름차순으로 출력한다.

 

 

[생각]

- 단순한 그래프 탐색 문제

 

 

[코드]

# 풀이 시간 : 15분
# 시간복잡도 : O(N+e)
# 공간복잡도 : O(N+e)
# 참고 : -
    # Python3는 시간초과
    
from collections import deque

def bfs(k):
    visited = [False] * (N + 1) # 계속 초기화 필요
    cnt = 0 # 연결된 간선 갯수 세기

    q = deque()
    q.append(k)
    visited[k] = True

    while q:
        x = q.popleft()

        for one in graph[x]:
            if not visited[one]: # 미방문 간선
                cnt += 1
                visited[one] = True
                q.append(one)

    return cnt


N, M = map(int, input().split())

graph = [[] for _ in range(N+1)]
for _ in range(M):
    a, b = map(int, input().split())
    #"B를 해킹하면, A도 해킹할 수 있다."
    #graph[a].append(b)
    graph[b].append(a)

computer = []
for i in range(1, N+1):
    computer.append([i, bfs(i)])

computer.sort(key = lambda x : -x[1]) # 간선이 가장 많이 연결된 컴퓨터 번호로, 내림차순 정렬

result = []
hackNum = computer[0][1] # 가장 많이 연결된 컴퓨터 번호
for a, b in computer:
    if hackNum == b:
        result.append(a)

print(*result)

 

저작자표시 (새창열림)

'Algorithm > Graph' 카테고리의 다른 글

[백준] 16624번 피리 부는 사나이 _ Python  (0) 2024.03.10
[백준] 11559번 Puyo Puyo _ Python  (0) 2024.02.01
[백준] 1600번 말이 되고픈 원숭이 _ Python  (0) 2023.11.27
[백준] 5547번 일루미네이션 _ Python  (0) 2023.11.27
[백준] 16918번 봄버맨 _ Python  (1) 2023.11.26
'Algorithms/Graph' 카테고리의 다른 글
  • [백준] 16624번 피리 부는 사나이 _ Python
  • [백준] 11559번 Puyo Puyo _ Python
  • [백준] 1600번 말이 되고픈 원숭이 _ Python
  • [백준] 5547번 일루미네이션 _ Python
wch_t
wch_t
  • wch_t
    끄적끄적(TIL)
    wch_t
  • 글쓰기 관리
  • 전체
    오늘
    어제
    • 분류 전체보기 (170)
      • Architecture (0)
      • Algorithm (67)
        • Math (5)
        • Simulation (1)
        • Data Structure (4)
        • DP (7)
        • Brute Fource (10)
        • Binary Search (6)
        • Greedy (2)
        • Graph (11)
        • Mst (1)
        • Shortest path (10)
        • Two Pointer (1)
        • Tsp (3)
        • Union Find (2)
        • Mitm (1)
      • CS (2)
        • 데이터베이스 (5)
        • 네트워크 (5)
      • DB (6)
      • DevOps (17)
        • AWS (9)
        • Docker (1)
        • CI-CD (5)
      • Error (1)
      • Project (0)
        • kotrip (0)
      • Spring (59)
        • 끄적끄적 (5)
        • 기본 (9)
        • MVC 1 (7)
        • MVC 2 (11)
        • ORM (8)
        • JPA 1 (7)
        • JPA 2 (5)
        • Spring Data Jpa (7)
      • Test (2)
      • TIL (5)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    백준 17289 파이썬
    spring-cloud-starter-aws-secrets-manager-config
    scope
    response_mode
    aws secrets manager
    docker
    Sxssf
    백준 3015 파이썬
    Merge
    form_post
    docker: not found
    Jenkins
    apache poi
    백준 17299 파이썬
    view algorithm
    spring-cloud-starter-bootstrap
    애플
    TempTable
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
wch_t
[백준] 1325번 효율적인 해킹 _ Python
상단으로

티스토리툴바