File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ https://www.acmicpc.net/problem/5567
3+ 1-> 2다리 이하 친구 관계만 초대 -> 총 인원수 파악
4+ 1-2-3-4-5
5+ """
6+ import sys
7+ from collections import deque
8+ #1.양방향 인접 리스트 만들기
9+ input = sys .stdin .readline
10+
11+ N = int (input ())
12+ M = int (input ())
13+ friends = [[] for _ in range (N + 1 )] # 1~ n 번까지
14+ for i in range (1 ,M + 1 ):
15+ x ,y = map (int , input ().split ())
16+ friends [x ].append (y )
17+ friends [y ].append (x )
18+
19+ #2. lv2 이하인 BFS 가즈아~
20+ start = 1
21+ q = deque ([[start ,0 ]]) # 학번, 관계 거리
22+ visited = [start ]
23+ answer = - 1
24+ while q :
25+ cnum , crelationship = q .popleft ()
26+ if crelationship >= 3 :
27+ break # 거리가3 넘으면 강제종료
28+ answer += 1
29+ # print(f"#{cnum} : {crelationship}")
30+ for nn in friends [cnum ] :
31+ if nn not in visited :
32+ q .append ([nn , crelationship + 1 ])
33+ visited .append (nn )
34+
35+
36+ # print(visited)
37+ print (answer )
You can’t perform that action at this time.
0 commit comments