-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11724_graph_BFS.cpp
More file actions
48 lines (40 loc) · 819 Bytes
/
11724_graph_BFS.cpp
File metadata and controls
48 lines (40 loc) · 819 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
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, m, ans = 0;
vector<vector<int>> graph(1001);
bool visited[1001];
queue<int> q;
int main()
{
cin >> n >> m;
int u, v;
for (int i = 0; i < m; i++)
{
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (int i = 1; i <= n; i++)
{
if (visited[i])
continue;
q.push(i);
while (!q.empty())
{
int cur = q.front();
q.pop();
visited[cur] = 1;
for (int next : graph[cur])
{
if (!visited[next])
q.push(next);
graph[cur].pop_back();
}
}
ans++;
}
cout << ans;
return 0;
}