Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define suni ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
- #define ll long long
- //#define tc int tc;cin>>tc;while(tc--)
- #define pb push_back
- #define endl "\n"
- #define spc " "
- #define yes "YES"
- #define no "NO"
- #define loop for(int i=0;i<n;i++) cin>>a[i]
- #define loop1 for(int i=0;i<n;i++) cin>>b[i]
- #define loop2 for(ll i=0;i<n;i++) cin>>c[i]
- #define beg begin()
- #define en end()
- // for determining positions// lower_bound( arr.begin(), arr.end(), ele)- arr.begin();
- // for determining positions// upper_bound( arr.begin(), arr.end(), ele)- arr.begin();
- // for finding elements // binary_search(arr.begin(), arr.end(), ele);
- // for sorting in vector// sort(a.begin(), a.end());
- vector<int>adj[1000090];
- void dfs (int node, vector<bool>&visited)
- {
- visited[node] = true;
- for(int i =0; i<adj[node].size(); i++)
- {
- if( visited[adj[node][i]] == false)
- {
- dfs(adj[node][i], visited);
- }
- }
- }
- int main()
- {
- suni;
- char c, a, b;
- int tc; cin>>tc;
- while(tc--){
- int nodes ;
- cin>>c;
- nodes = c - '0' - 16;
- cin.ignore();
- string s;
- while(getline(cin, s) && s!= "")
- {
- adj[s[0] - '0'- 16].push_back(s[1]-'0' - 16);
- adj[s[1] - '0' - 16].push_back(s[0] - '0' - 16);
- }
- vector<bool>visited(nodes+1, false);
- int conn = 0;
- for(int i =1; i<= nodes; i++)
- {
- if(! visited[i]){
- conn++;
- dfs(i, visited);
- }
- }
- cout<<conn<<endl;
- if(tc) cout<<endl;
- for(int i =1; i<=nodes; i++) adj[i].clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement