Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- ll n,m;
- cin>>n>>m;
- vector<ll> adj[n+1];
- bool visited[n+1] = {0};
- for (ll i=1,x,y; i<=m; i++){
- cin>>x>>y;
- adj[x].push_back(y);
- adj[y].push_back(x);
- }
- queue<ll> q;
- ll ans=0;
- for (ll i=1; i<=n; i++){
- if (!visited[i]){
- ans++;
- visited[i]=true;
- q.push(i);
- while(!q.empty()){
- ll s=q.front();
- q.pop();
- for (auto u:adj[s]){
- if (visited[u]) continue;
- visited[u]=true;
- q.push(u);
- }
- }
- }
- }
- cout<<ans<<"\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement