File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #2023-05-11-Week5-수업
2+ #5567_결혼식
3+
4+ '''
5+ 입력 값
6+ 6
7+ 5
8+ 1 2
9+ 1 3
10+ 3 4
11+ 2 3
12+ 4 5
13+ '''
14+
15+ #bfs
16+
17+ import sys
18+ from collections import deque
19+
20+ '''
21+ 이렇게 작성했더니 런타임에러
22+ input = sys.stdin.readline()
23+ N = int(input())
24+ M = int(input())
25+ '''
26+
27+ N = int (sys .stdin .readline ())
28+ M = int (sys .stdin .readline ())
29+
30+ graph = [[False ] * (N + 1 ) for i in range (N + 1 )] #False 대신 0 가능 #1번 인덱스부터 사용
31+
32+ for i in range (M ):
33+ a , b = map (int , sys .stdin .readline ().split ())
34+ graph [a ].append (b ) # a 인덱스에 b 넣기
35+ graph [b ].append (a ) # b 인덱스에 a 넣기
36+
37+ #1번 인덱스 -> 본인 -> 본인의 친구
38+
39+ visited = [0 ] * (N + 1 ) # false 대신 0 이어야 함 -> 덧셈 연산 수행을 위해 int형
40+
41+ def bfs (vertex ):
42+ queue = deque ([vertex ])
43+ visited [vertex ] = 1
44+ while queue : #queue==1일 때 반복
45+ vertex = queue .popleft () #deque의 popleft()은 list의 pop(0)와 같지만 O(1)임
46+ for i in graph [vertex ]:
47+ if visited [i ] == 0 :
48+ queue .append (i )
49+ visited [i ] = visited [vertex ] + 1
50+ bfs (1 )
51+ result = 0 #초대하는 동기의 수
52+ for i in range (2 ,N + 1 ):
53+ if (visited [i ] != 0 ) and (visited [i ] < 4 ) : # 본인/친구/친구의 친구 -> visited 수 범위 1~3
54+ #if 0 < visited[i] <=3 : 이게 더 간결함
55+ result += 1 #visited 범위에 해당하면 누적합 계산
56+ print (result )
You can’t perform that action at this time.
0 commit comments