-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9372.py
More file actions
38 lines (31 loc) · 716 Bytes
/
9372.py
File metadata and controls
38 lines (31 loc) · 716 Bytes
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
32
33
34
35
36
37
38
from collections import deque
import sys
input = sys.stdin.readline
def bfs(x):
q = deque()
q.append(x)
c[x] = 1 # 방문
cnt = 0
while q :
x = q.popleft()
for nx in a[x]:
if c[nx] == 0:
c[nx] = 1
cnt += 1
q.append(nx)
return cnt
testcase = int(input()) #tc개수
while testcase:
n,m = map(int,input().split())
a = [ [] for _ in range(n)]
c = [0 for _ in range(n)]
for _ in range(m):
x,y = map(int,input().split())
a[x-1].append(y-1)
a[y-1].append(x-1)
ans = 0
for i in range(n):
if c[i] == 0:
ans += bfs(i)
print(ans)
testcase -= 1