要求实现函数,能够统计并返回无向图(顶点数不超过100)的连通分量(极大连通子图)数,可增加自定义函数或借助STL(标准模板库)之queue(队列)。无向图采用邻接矩阵存储,输入的顶点从1开始编号,但顶点下标从0开始使用。
函数接口定义:
int CountSubGraph(int mat[][N], int n);
其中参数 mat
表示邻接矩阵, n
表示顶点总数。
裁判测试程序样例:
#include <iostream>
#include <queue>
using namespace std;
const int N=100;
int CountSubGraph(int mat[][N], int n);
int main() {int n,m,a,b,i;cin>>n>>m; //输入顶点数n和边数mint mat[N][N]; //定义邻接矩阵fill(&mat[0][0], &mat[n-1][n-1]+1, 0);for(i=0; i<m; i++) {//输入m条边建图cin>>a>>b; //输入两个顶点a、b,表示a、b之间有一条边a--;b--;mat[a][b]=1;mat[b][a]=1;}cout<<CountSubGraph(mat,n)