-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11724.cpp
More file actions
51 lines (48 loc) · 945 Bytes
/
11724.cpp
File metadata and controls
51 lines (48 loc) · 945 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
49
50
51
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int N, M, from, to;
vector<vector<int>> graph;
bool visited[1001];
void bfs(int idx)
{
queue<int> q;
q.push(idx);
visited[idx] = true;
while (!q.empty())
{
int current = q.front();
q.pop();
for (int i = 0; i < graph[current].size(); ++i)
{
int next = graph[current][i];
if (!visited[next])
{
q.push(next);
visited[next] = true;
}
}
}
}
int main()
{
cin >> N >> M;
graph.resize(N + 1);
for (int i = 0; i < M; ++i)
{
cin >> from >> to;
graph[from - 1].push_back(to - 1);
graph[to - 1].push_back(from - 1);
}
int answer = 0;
for (int i = 0; i < N; ++i)
{
if (!visited[i])
{
bfs(i);
answer++;
}
}
cout << answer;
}