Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- const int maxn= 1e5+5;
- int n,m;
- int check[maxn];
- vector <int> e[maxn];
- stack<int> s;
- queue <int> q;
- int ans=0;
- /*void dfs(int x){
- //while(!s.empty()) s.pop();
- s.push(x);
- check[x]=1;
- while(!s.empty()){
- int u=s.top();
- s.pop();
- //cout<<u<<" ";
- for (int v: e[u])
- if (check[v]==0){
- check[v]=1;
- s.push(v);
- }
- }
- }*/
- void bfs(int x)
- {
- q.push(x);
- check[x]=1;
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- //cout<<u<<" ";
- for (int v: e[u])
- if (check[v]==0)
- {
- check[v]=1;
- q.push(v);
- }
- }
- }
- int main()
- {
- cin>>n>>m;
- for (int i=1; i<=m; i++)
- {
- int u,v;
- cin>>u>>v;
- e[u].push_back(v);
- e[v].push_back(u);
- }
- //dfs(1);
- //bfs(1);
- for (int i=1; i<=n; i++)
- {
- if (check[i]==0)
- {
- ans++;
- dfs(i);
- //bfs(i);
- }
- }
- cout<<ans<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement