-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdfs_short.cpp
More file actions
49 lines (48 loc) · 872 Bytes
/
dfs_short.cpp
File metadata and controls
49 lines (48 loc) · 872 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
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;
vector<int> adj[100001];
int vis[100001]={0};
void dfs(int v)
{
vis[v]=1;
for(auto x:adj[v])
{
if(vis[x]==0)
dfs(x);
}
return;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ll t=1;
while(t--)
{
int n,q;
cin>>n>>q;
while(q--)
{
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int c=0;
for(int i=0;i<n;i++)
{
if(!vis[i])
{
dfs(i);
c++;
}
}
cout<<c<<endl;
}
return 0;
}