Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- const int MN=1e5+10;
- bool visited[MN];
- vector<int> adj_list[MN];
- void dfs(int x){
- visited[x]=true;
- for (int u:adj_list[x]){
- if (!visited[u]){
- dfs(u);
- }
- }
- }
- int connected_components(){
- int counter=0;
- for (int i=1; i<=n; i++){
- if (!visited[i]){
- counter++;
- dfs(i);
- }
- }
- return counter;
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--){
- cin>>n;
- int a[100][100];
- for (int i=1; i<=n; i++){
- for (int j=1; j<=n; j++){
- cin>>a[i][j];
- if (a[i][j]==1){
- adj_list[i].push_back(j);
- }
- }
- }
- cout<<connected_components<<"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement