-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11724.cpp
More file actions
62 lines (54 loc) · 975 Bytes
/
11724.cpp
File metadata and controls
62 lines (54 loc) · 975 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
61
62
#include <bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vint;
const int INF = 0x3f3f3f3f; const int mINF = 0xc0c0c0c0;
const ll LINF = 0x3f3f3f3f3f3f3f3f; const ll mLINF = 0xc0c0c0c0c0c0c0c0;
int ans;
int N,M;
vint adj[1001];
int vi[1001];
void bfs(int st) {
queue<int> q;
q.push(st);
vi[st] = 1;
while(!q.empty()) {
int curr = q.front(); q.pop();
for(int i=0;i<adj[curr].size();++i) {
int next = adj[curr][i];
if(!vi[next]) {
q.push(next);
vi[next] = 1;
}
}
}
return;
}
void sol() {
for(int i=1;i<=N;++i) {
if(adj[i].size() > 0 && !vi[i]) {
bfs(i);
ans++;
}
}
for(int i=1;i<=N;++i) {
if(adj[i].size() == 0 && !vi[i]) {
ans++;
}
}
return;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
int u,v;
for(int i=0;i<M;++i) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
sol();
cout << ans << '\n';
return 0;
}