-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneps309.cpp
More file actions
49 lines (36 loc) · 742 Bytes
/
Copy pathneps309.cpp
File metadata and controls
49 lines (36 loc) · 742 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
/*
* Contest : Neps
* Problem : 309 - Gincana (OBI 2011)
* Link : https://neps.academy/br/exercise/309
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
vector<vector<int>> adj;
vector<int> visited;
void dfs(int no) {
visited[no] = true;
for (auto &v : adj[no])
if (!visited[v]) dfs(v);
}
int main() {
fastio
int n, m, a, b, cnt = 0;
cin >> n >> m;
adj.assign(n + 1, vector<int>());
visited.assign(n + 1, false);
while (m--) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
dfs(i);
cnt++;
}
}
cout << cnt << '\n';
return 0;
}