先判断图是否连通,非连通图则是非欧拉图。 对无向连通图而言,若所有结点的度都是偶数,则该图为欧拉图。 对无向连通图而言,若只有两个结点的度数是奇数,其余结点度数为偶数,则该图为半欧拉图。 判断一个图是否欧拉图 若是欧拉图,则输出"a is euler”; 若是半欧拉图,则输出"a is semi-euler”; 若不是欧拉图,则输出“a is not euler”。 注意:该输出前后无空格,行尾回车。 并补充下列代码: #include <stdio.h> const int N = 10; //判断无向图是否欧拉图 //返回值为奇度数结点数 int euler1(int** a, int n) { } 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);//读入无向边 } 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"); }
时间: 2024-01-19 14:04:55 浏览: 98
你的代码中缺少了判断无向图是否连通的部分。接下来我给你补充完整代码:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int N = 10;
// 判断无向图是否连通
bool connected(int** a, int n) {
bool* visited = new bool[n];
memset(visited, false, n * sizeof(bool));
int* queue = new int[n];
int head = 0, tail = 0;
queue[tail++] = 0;
visited[0] = true;
while (head < tail) {
int u = queue[head++];
for (int v = 0; v < n; v++) {
if (a[u][v] && !visited[v]) {
visited[v] = true;
queue[tail++] = v;
}
}
}
for (int i = 0; i < n; i++) {
if (!visited[i]) {
return false;
}
}
return true;
}
// 返回值为奇度数结点数
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 != 0) {
odd++;
}
}
return odd;
}
int main() {
int** a;// 无向图
int n, i, j;
scanf("%d", &n);// 读入结点数
if (n > N) {
printf("error\n");
return 0;
}
a = new int* [n];
for (i = 0; i < n; i++) {
a[i] = new int[n];
for (j = 0; j < n; j++) {
a[i][j] = 0;
}
}
while (1) {
scanf("%d%d", &i, &j);// 读入无向边
if (i < 0 || j < 0) {
break;
}
if (i >= n || j >= n) {
printf("error\n");
return 0;
}
a[i][j] = a[j][i] = 1;
}
if (!connected(a, n)) {
printf("a is not euler\n");
return 0;
}
int odd = euler1(a, 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");
}
return 0;
}
```
阅读全文