【无向图欧拉回路判定】欧拉回路

欧拉回路

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10643    Accepted Submission(s): 3881


Problem Description
欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
 

 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结
束。
 

 

Output
每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
 

 

Sample Input
3 3
1 2
1 3
2 3
3 2
1 2
2 3
0
 

 

Sample Output
1
0
 
  代码:超级水题(2015.8.13)
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 #define Max 1010
 7 char Str[Max];
 8 int EDG_Count;/*统计边数*/
 9 int ID[Max];/*并查集判断是否为同一幅图*/
10 //int InD[M];/*入度*/
11 //int OuD[M];/*出度*/
12 int DuD[Max];
13 int Find(int x)
14 {
15     if(x!=ID[x])ID[x]=Find(ID[x]);
16     return ID[x];
17 }
18 void Cread(int N)
19 {
20     for(int i=0;i<=N;i++)
21     {
22         ID[i]=i;DuD[i]=0;
23     }EDG_Count=0;
24 }
25 void Add(int a,int b)
26 {
27     int A=Find(a);
28     int B=Find(b);
29     if(A!=B){ID[A]=B;EDG_Count++;}
30 }
31 
32 void Update(int a,int b)
33 {
34     Add(a,b);
35     DuD[a]++;DuD[b]++;
36     //OuD[a]++;InD[b]++;
37 }
38 
39 int main()
40 {
41     int T,N,M,i,a,b,sign1,sign2,sign3;
42     while(scanf("%d",&N)&&N)
43     {
44         Cread(N);
45         scanf("%d",&M);
46         for(i=1;i<=M;i++)
47         {
48             scanf("%d%d",&a,&b);
49             Update(a,b);
50         }
51         sign1=sign2=0;
52         for(i=1;i<=N;i++)
53         {
54             if(DuD[i]%2==0)sign2++;
55             else sign1++;
56         }
57         if(EDG_Count!=N-1)/*是否构成一幅图*/
58             {printf("0\n");continue;}
59         if(sign1==2&&sign2==N-2)/*链的情况,题目要求*/
60             printf("0\n");
61         else if(sign1==0&&sign2==N) /*环的情况*/
62             printf("1\n");
63         else
64             printf("0\n");
65     }
66     return 0;
67 }
View Code

 

转载于:https://www.cnblogs.com/Wurq/articles/4728750.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于无向图欧拉回路判定条件是所有顶点的度数都为偶数,因此可以通过统计每个顶点的度数来判断是否存在欧拉回路。对于有向图,欧拉回路判定条件是每个顶点的入度等于出度,因此需要统计每个顶点的入度和出度。以下是用C语言实现的无向图和有向图欧拉回路判定代码: ```c #include <stdio.h> #define MAXN 1000 int G[MAXN][MAXN]; // 邻接矩阵表示图 int degree[MAXN]; // 存储每个顶点的度数(无向图)或入度减出度(有向图) int isEulerian(int n, int isDirected) { int i, j, odd = 0; for (i = 0; i < n; i++) { degree[i] = 0; for (j = 0; j < n; j++) { if (G[i][j]) { degree[i]++; } } if (degree[i] % 2 == 1) { odd++; } } if (isDirected) { for (i = 0; i < n; i++) { degree[i] = 0; for (j = 0; j < n; j++) { if (G[i][j]) { degree[i]++; degree[j]--; } } if (degree[i] != 0) { return 0; } } } return (odd == 0); } int main() { int n, m, i, j, a, b, isDirected; scanf("%d%d%d", &n, &m, &isDirected); for (i = 0; i < m; i++) { scanf("%d%d", &a, &b); G[a][b] = G[b][a] = 1; // 无向图边的两个方向都要标记 if (isDirected) { G[a][b] = 1; // 有向图只需要标记一次 } } if (isEulerian(n, isDirected)) { printf("Exist Eulerian circuit\n"); } else { printf("Not exist Eulerian circuit\n"); } return 0; } ``` 其中,`G` 是一个大小为 `n` 的邻接矩阵,如果 `G[i][j]` 为 `1`,则表示存在从顶点 `i` 到顶点 `j` 的边。`degree` 数组用于存储每个顶点的度数或入度减出度。`isEulerian` 函数判断是否存在欧拉回路,其中 `n` 表示顶点数,`isDirected` 表示是否为有向图,返回值为 `1` 则表示存在欧拉回路,否则不存在。在 `main` 函数中,首先读入图的信息,然后调用 `isEulerian` 函数判断是否存在欧拉回路,并输出结果。 需要注意的是,对于无向图,如果存在欧拉回路,则可以随便从一个顶点出发,遍历整个图;对于有向图,则需要从一个入度等于出度的顶点出发,遍历整个图。因此,如果存在欧拉回路,还需要找到一个起点,并输出遍历路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值