欧拉回路:
1.所有顶点度为奇数
2.为连通图
1 #include<iostream> 2 #include<string> 3 #include <cstring> 4 using namespace std; 5 int g[1001][1001];//存顶点 6 int visited[1001] = { 0 }; 7 int cnt = 0;//dfs执行时计数,如果最后的cnt等于顶点数,说明连通 8 int degree[1001] = { 0 };//储存顶点的度 9 int a, b;//顶点数和边数 10 void dfs(int v)//判断图是否连通 11 { 12 if (visited[v] == 1)return; 13 visited[v] = 1; 14 cnt++; 15 for (int i = 1; i <= a; i++) 16 { 17 if ((g[v][i] == 1 || g[i][v] == 1) && visited[i] == 0) 18 dfs(i); 19 } 20 } 21 int main() 22 { 23 int flag;//判断顶点的度中是否有奇数,1表示全为偶数 24 cin >> a >> b; 25 for (int i = 1; i <= a; i++) 26 { 27 for (int j = 1; i <= a; i++) 28 { 29 g[i][j] = g[j][i] = 0; 30 } 31 } 32 for (int i = 0; i < b; i++) 33 { 34 int x, y; cin >> x >> y; 35 g[x][y] = g[y][x] = 1; 36 degree[x]++; degree[y]++; 37 if (degree[x] % 2 != 0)flag = 0; else flag = 1; 38 if (degree[y] % 2 != 0)flag = 0; else flag = 1; 39 } 40 dfs(1); 41 if (cnt == a && flag == 1) 42 { 43 cout << "1"; 44 } 45 else 46 cout << "0"; 47 return 0; 48 }