问题描述:
无向图,计算图的最大度、最小度和连通分支数。
图的最大度指的是图中所有结点的度的最大值,图的最小度指的是图中所有结点的度的最小值。
输入
第一行是一个整数V,表示图有V个结点,结点编号0, 1, 2, …, V-1。(1<=V<=1万)
第二行是一个整数E,表示图中有E条边。(0<=E<=10万)
接下来有E行,每行表示一条边,用空格分隔的两个整数表示边的两个端点。
请注意选择合适的数据结构。
输出
三行,分别输出图的最大度、最小度和连通分支数。
样例输入
13
13
0 5
4 3
0 1
9 12
6 4
5 4
0 2
11 12
9 10
0 6
7 8
9 11
5 3
样例输出
4
1
3
代码存在部分问题,CG平台8组数据只能通过四组,50分,代码是百度找了个好歹改了改,参考代码下面会跟着给出
#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
/**
加static防止报[Warning] anonymous type with no linkage used to declare variable '<anonymous struct> Node [100000]' with linkage
*/
static struct
{
int sonList[10000];
int sonNum;
int vir;
}Node[10000];
int num, nx, ny, branch = 0, countNum = 1, a[10000], line;
void BFS(int x)
{
queue<int>q;
int k0 = x, k1;
// int flag = 0;
Node[k0].vir = 1;
q.push(k0);
while (!q.empty())//打开注释怎打印所有的连通分支
{
k0 = q.front();
q.pop();
//if (flag)
// cout << "-";
//flag = 1;
//cout << k0;
if (Node[k0].sonNum == 0)
continue;
sort(Node[k0].sonList, Node[k0].sonList + Node[k0].sonNum);
for (int i = 0; i <= Node[k0].sonNum; i++)
{
k1 = Node[k0].sonList[i];
if (Node[k1].vir == 1)
continue;
q.push(k1);
Node[k1].vir = 1;
}
}
//cout << endl;
branch++;
}
int main()
{
cin >> num;
for (int i = 0; i < num; i++)
{
Node[i].sonNum = 0;
Node[i].vir = 0;
}
cin >> line;
while (1)
{
cin >> nx;
countNum++;
//if (nx == -1)//通过-1来结束循环输入
//break;
if(countNum >= line*2)
break;
cin >> ny;
countNum++;
Node[nx].sonList[Node[nx].sonNum] = ny;
Node[nx].sonNum++;
Node[ny].sonList[Node[ny].sonNum] = nx;
Node[ny].sonNum++;
}
for (int i = 0; i < num; i++) //注意其他分支树
{
if (Node[i].vir == 0)
BFS(i);
}
for(int i = 0; i < num ; i++){
a[i] = Node[i].sonNum;
}
sort(a,a+num);
cout << a[num-1] <<endl;
cout << a[0] <<endl;
cout << branch <<endl;
return 0;
}
参考代码:
求解无向图的各连通分支
输入:
第一行为图的节点数n(节点编号0至n-1,0< n<=10)
从第二行开始列出图的边,-1表示输入结束
输出:
输出每个连通分支的广度优先搜索序列(从连通分支的最小编号开始),不同分支以最小编号递增顺序列出
sample:
input:
8
0 5
5 2
4 5
5 6
6 2
3 7
0 2
-1
output:
0-5-2-4-6
1
3-7
测试用例
用例1:
输入:
8
0 5
5 2
4 5
5 6
6 2
3 7
-1
输出:
0-5-2-4-6
1
3-7
用例2:
输入:
10
1 2
7 2
8 5
2 8
5 7
6 8
6 5
9 0
0 3
-1
输出:
0-3-9
1-2-7-8-5-6
4
用例3:
输入:
5
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
3 4
-1
输出:
0-1-2-3-4
解析:注意题目描述用的用例有问题,应该按测试用例的输出。对于非连通分支树,就是对节点循环输出即可,输出过的标记一下。
#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
struct
{
int sonList[100];
int sonNum;
int vir;
}Node[100];
int num, nx, ny;
void BFS(int x)
{
queue<int>q;
int k0 = x, k1, flag = 0;
Node[k0].vir = 1;
q.push(k0);
while (!q.empty())
{
k0 = q.front();
q.pop();
if (flag)
cout << "-";
flag = 1;
cout << k0;
if (Node[k0].sonNum == 0)
continue;
sort(Node[k0].sonList, Node[k0].sonList + Node[k0].sonNum);
for (int i = 0; i <= Node[k0].sonNum; i++)
{
k1 = Node[k0].sonList[i];
if (Node[k1].vir == 1)
continue;
q.push(k1);
Node[k1].vir = 1;
}
}
cout << endl;
}
int main()
{
cin >> num;
for (int i = 0; i < num; i++)
{
Node[i].sonNum = 0;
Node[i].vir = 0;
}
while (1)
{
cin >> nx;
if (nx == -1)
break;
cin >> ny;
Node[nx].sonList[Node[nx].sonNum] = ny;
Node[nx].sonNum++;
Node[ny].sonList[Node[ny].sonNum] = nx;
Node[ny].sonNum++;
}
for (int i = 0; i < num; i++) //注意其他分支树
{
if (Node[i].vir == 0)
BFS(i);
}
return 0;
}