解法:
dfs求连通性
1.设节点表vis[]
2.遍历节点表dfs标记,每次得到一个连通分量
#include<iostream>
#include<vector>
using namespace std;
int arr[100][100];
void dfs(vector<bool>& vis, int v) {//不用终止条件(自带)vis[v] = true;for (int i = 0; i < vis.size(); i++) {if (arr[v][i] && !vis[i]) {dfs(vis, i);}}
}
int main() {int n, cnt = 0;cin >> n;vector<bool> vis(n, false);for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cin >> arr[i][j];}}for (int i = 0; i < vis.size(); i++) {if (!vis[i]) {cnt++;dfs(vis, i);}}cout << cnt;return 0;
}