본문 바로가기

파이썬

[백준 파이썬] 1325번 효율적인 해킹

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

 

1325번: 효율적인 해킹

첫째 줄에, N과 M이 들어온다. N은 10,000보다 작거나 같은 자연수, M은 100,000보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에 신뢰하는 관계가 A B와 같은 형식으로 들어오며, "A가 B를 신뢰한

www.acmicpc.net

 

<코드>

import sys
from collections import deque
input = sys.stdin.readline

def bfs(n):
    q = deque([n])
    cnt = 1
    visited = [0]*(N+1)
    visited[n] = 1
    while q:
        c = q.popleft()
        for i in arr[c]:
            if not visited[i]:
                visited[i] = 1
                q.append(i)
                cnt += 1
    return cnt

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

arr = [[] for _ in range(N+1)]

for _ in range(M):
    a, b = map(int, input().split())
    arr[b].append(a)

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

max_v = max(answer)

for i in range(1, N+1):
    if answer[i] == max_v:
        print(i, end=' ')

 

<풀이방법>

https://xxuna.tistory.com/20

 

[BAEKJOON] 1325 효율적인 해킹 Python 파이썬

헛짓거리 짱 많이함!! 1. 메모리 초과 난 코드 def find_set(x): if p[x] == x: return x else: return find_set(p[x]) def union(a, b): p_a = find_set(a) p_b = find_set(b) p[p_b] = p_a N,M = map(int,input..

xxuna.tistory.com