Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int mod=1e9 + 7;
- main()
- {
- int t;cin>>t;
- while(t--){
- int n,e;cin>>n>>e;
- int u,v;
- vector<vector<int>>g(n);
- bool vis[n];
- memset(vis,false,sizeof(vis));
- for(int i=0;i<e;i++){
- cin>>u>>v;
- g[u].push_back(v);
- g[v].push_back(u);
- }
- // for(int i=0;i<n;i++){
- // cout<<i<<":->";
- // for(auto it:g[i])
- // cout<<it<<",";
- // cout<<endl;
- // }
- queue<int>q;
- int ans=0;
- for(int i=0;i<n;i++){
- if(vis[i]==false){
- ans++;
- q.push(i);
- while(!q.empty()){
- int node=q.front();
- q.pop();
- // cout<<node<<" ";
- vis[node]=true;
- for(auto it:g[node]){
- if(vis[it]==false){
- q.push(it);
- }
- }
- }
- }
- }
- // cerr<<"ans:"<<ans<<endl;
- cout<<ans<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement