要求如下:先判断图是否连通,非连通图则是非欧拉图。 对无向连通图而言,若所有结点的度都是偶数,则该图为欧拉图。 对无向连通图而言,若只有两个结点的度数是奇数,其余结点度数为偶数,则该图为半欧拉图。 判断一个图是否欧拉图 若是欧拉图,则输出"a is euler”; 若是半欧拉图,则输出"a is semi-euler”; 若不是欧拉图,则输出“a is not euler”。 注意:该输出前后无空格。按要求完善代码:#include <stdio.h> const int N = 10; //判断无向图是否欧拉图 //返回值为奇度数结点数 int euler1(int** a, int n) { //begin************* //end**************** } int main( ) { int** a1;//无向图 int n, i, j; scanf("%d", &n);//读入结点数 if (n > N) { printf("error\n"); return 0; } a1 = new int* [n]; for (i = 0; i < n; i++) { a1[i] = new int[n]; for (j = 0; j < n; j++) { a1[i][j] = 0; } } while (1) { scanf("%d%d", &i, &j);//读入无向边 //begin***** //end*********** } int odd = euler1(a1, n); if (odd == 0) { printf("a is euler\n"); } else if (odd == 2) printf("a is semi-euler\n"); else printf("a is not euler\n"); }
时间: 2023-07-23 10:43:00 浏览: 66
代码如下:
```
#include <stdio.h>
const int N = 10;
//返回值为奇度数结点数
int euler1(int** a, int n) {
int odd = 0;//奇度数结点数
for (int i = 0; i < n; i++) {
int degree = 0;//当前结点的度数
for (int j = 0; j < n; j++) {
degree += a[i][j];
}
if (degree % 2 == 1) odd++;
}
return odd;
}
int main( ) {
int** a1;//无向图
int n, i, j;
scanf("%d", &n);//读入结点数
if (n > N) {
printf("error\n");
return 0;
}
a1 = new int* [n];
for (i = 0; i < n; i++) {
a1[i] = new int[n];
for (j = 0; j < n; j++) {
a1[i][j] = 0;
}
}
while (1) {
scanf("%d%d", &i, &j);//读入无向边
if (i == -1 && j == -1) break;
if (i < 0 || i >= n || j < 0 || j >= n) {
printf("error\n");
return 0;
}
a1[i][j]++;
a1[j][i]++;
}
int odd = euler1(a1, n);
if (odd == 0) {
printf("a is euler");
}
else if (odd == 2) {
printf("a is semi-euler");
}
else {
printf("a is not euler");
}
return 0;
}
```
相关推荐
![cs](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)