-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneps512.cpp
More file actions
60 lines (47 loc) · 949 Bytes
/
Copy pathneps512.cpp
File metadata and controls
60 lines (47 loc) · 949 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
52
53
54
55
56
57
58
59
60
/*
* Contest : OBI 2013 - Fase 2
* Problem : Famílias de Troia
* Link : https://neps.academy/br/exercise/512
*/
#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
vector<vector<int>> adj;
vector<bool> visited;
void bfs(int no) {
queue<int> fila;
visited[no] = true;
fila.push(no);
while (!fila.empty()) {
int atual = fila.front();
fila.pop();
for (auto v : adj[atual]) {
if (!visited[v]) {
fila.push(v);
visited[v] = true;
}
}
}
}
int main() {
fastio
int n, m;
scanf("%i%i", &n, &m);
adj.assign(n + 1, vector<int>());
visited.assign(n + 1, false);
int a, b;
for (int i = 0; i < m; i++) {
scanf("%i%i", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
bfs(i);
cnt++;
}
}
printf("%i\n", cnt);
return 0;
}