-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11724.cpp
More file actions
58 lines (55 loc) · 1.27 KB
/
11724.cpp
File metadata and controls
58 lines (55 loc) · 1.27 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
#define SETTING ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std ;
vector< vector<int> > arr(1001) ;
bool visited[1001] ;
int V, E ;
void InputSetting()
{
cin >> V >> E ;
int start, dest ;
for(int i = 0 ; i < E ; i++)
{
cin >> start >> dest ;
arr[start].push_back(dest) ;
arr[dest].push_back(start) ;
}
}
int BFS()
{
int cnt = 0 ;
queue<int> que ;
for(int i = 1 ; i <= V ; i++)
{
if(!visited[i])
{
cnt++ ;
for(int j = 0; j < arr[i].size(); j++)
{
if(!visited[arr[i][j]]) que.push(arr[i][j]) ;
visited[arr[i][j]] = 1 ;
}
while(!que.empty())
{
int que_size = que.size() ;
for(int k = 0 ; k < arr[que.front()].size() ; k++)
{
int temp = arr[que.front()][k] ;
if(!visited[temp])
{
visited[temp] = 1 ;
que.push(temp) ;
}
}
que.pop() ;
}
}
}
return cnt ;
}
int main()
{
SETTING ;
InputSetting() ;
cout << BFS() ;
}