判断图是否联通

~~~ c++

int graph[30][30];   //无向邻接矩阵

int v_c[30];      //是否访问过

void dfs(int u){
for(int i = 0; i < 26; i++){
if(graph[u][i] && !v_c[i]){
v_c[i] = 1;
DFS(i);
}
}
}
bool check(){
int count = 0;
for(int i = 0; i < 26; i++){
if(in[i] || out[i]){
if(!v_c[i]){
count++;
v_c[i] = 1;
dfs(i);
}
}
}
  //当count>1 说明不是连通图
if(count > 1) return false;
return true;
}

~~~

posted @ 2021-03-08 23:23  MmmelodyyY  阅读(131)  评论(0)    收藏  举报