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
- const ll nmax=1e6+7;
- bool visited[nmax]={0};
- ll counter=0;
- vector <ll> adj[nmax];
- void clearvector(){
- for (int i=1; i<nmax; i++){
- adj[i].clear();
- }
- }
- void dfs(ll k){
- visited[k]=true;
- for (auto u:adj[k]){
- if (!visited[u]){
- dfs(u);
- }
- }
- }
- void connected(ll x){
- if (!visited[x]){
- counter++;
- dfs(x);
- }
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--){
- ll n,m;
- cin>>n>>m;
- for (ll i=0; i<m; i++){
- ll x,y;
- cin>>x>>y;
- adj[x].push_back(y);
- adj[y].push_back(x);
- }
- for (ll i=1; i<=n; i++){
- connected(i);
- }
- cout<<counter<<"\n";
- clearvector();
- visited[nmax]={0};
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement