-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
36 lines (33 loc) · 734 Bytes
/
Copy pathmain.cpp
File metadata and controls
36 lines (33 loc) · 734 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
#include <cstdio>
#include <vector>
using namespace std;
vector<int> graph[1001];
bool check[1001];
void dfs(int start) {
check[start] = true;
for(int i = 0; i < graph[start].size(); i++) {
int next = graph[start][i];
if(check[next] == false) {
check[next] = true;
dfs(next);
}
}
}
int main() {
int N, M;
scanf("%d %d", &N, &M);
for(int i = 0; i < M; i++) {
int u, v;
scanf("%d %d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
int component = 0;
for(int i = 1; i <= N; i++) {
if(check[i] == false) {
dfs(i);
component++;
}
}
printf("%d\n", component);
}