UVa 10596 Moring Walk【欧拉回路】

题意:给出n个点,m条路,问能否走完m条路。

自己做的时候= =三下两下用并查集做了交,WA了一发-后来又WA了好几发--(而且也是判断了连通性的啊)

搜了题解= = 发现是这样的:

因为只要求走完所有的路,即为只需要走完已经给出的路,而并没有要求所走得路上含有所有的点,

比如说 给出的路有这些

0 1

1 2

2 3

3 0

4 4

那么构成的路即为,绕着图中的蓝色线走一圈,即为走完了所有的路,

而4是一个孤立点,也并没有构成路,所以不需要管它

代码中的 if(d[i]!=0)是判断这个点是否是孤立点的

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int d[10010],pre[10010];
 7 int find(int root){ return root == pre[root] ? root : pre[root] = find(pre[root]); }
 8 void unionroot(int x,int y)
 9 {
10     int root1=find(x);
11     int root2=find(y);
12     if(root1!=root2) pre[root1]=root2;
13 }
14
15 int main()
16 {
17     int m,n,u,v,i;
18     while(scanf("%d %d",&n,&m)!=EOF)
19     {
20             int flag=1;
21         memset(d,0,sizeof(d));
22         memset(pre,0,sizeof(pre));
23         for(i=0;i<=10010;i++)
24         pre[i]=i;
25         for(i=1;i<=m;i++)
26         {
27             scanf("%d %d",&u,&v);
28             d[u]++;
29             d[v]++;
30             unionroot(u,v);
31         }
32
33         int root=find(0);
34
35         if(m==0||n==0) flag=0;//n=0的时候是不能构成回路的,m=0的时候 也不能
36
37         for(i=0;i<n;i++){
38                 if(d[i]!=0){ //判断这个点是否是孤立的点
39                     if(find(i)!=root||d[i]%2!=0){ //判断这个点是否在同一个联通块上,以及判断这个店的度数是否为偶数
40                     flag=0;
41                     break;
42                   }
43                 }
44         }
45
46         if(flag)
47         printf("Possible\n");
48         else
49         printf("Not Possible\n");
50     }
51     return 0;
52 }

她只是想走完所有的路,她不想走4

go---go---

时间: 2024-08-04 10:51:54

UVa 10596 Moring Walk【欧拉回路】的相关文章

uva 10596 Morning Walk(欧拉回路)

这道题是神坑啊,花了我将近3个小时,本来敲的挺顺的,交上去就是wa,坑点真坑,原来是有的路口交叉点可以没有 路通向它,无语,没有路通向也可以叫交叉点....以后一定得考虑多种情况,用了bfs(vector做的)和dfs判断 的是否连通,判断连通之后只需要再判断是否都有偶数个度就ok了,坑,真坑. bfs代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #i

uva 10596 Morning Walk (欧拉回路)

uva 10596 Morning Walk Kamal is a Motashota guy. He has got a new job in Chittagong . So, he has moved to Chittagong from Dinajpur. He was getting fatter in Dinajpur as he had no work in his hand there. So, moving to Chittagong has turned to be a ble

UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)

UVA - 10596 Morning Walk Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chittagong . So, he has moved to Ch

uva 10596 - Morning Walk

Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chittagong. So, he has moved to Chittagong from Dinajpur. He was getting fatter in Dinajpur as he had no work in his hand there. So, moving to Chittagong ha

uva 503 - Parallelepiped walk(几何)

题目链接:uva 503 - Parallelepiped walk 恶心题,将三维转成两维,直线距离最短,WA了一天.假设起点在地面,除了考虑经过0,1个面的可能,还要考虑经过两个面到达的可能.后面提供一个生成数据的代码. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const ll inf = 0x3f3f3f

uva 10054 The Necklace 欧拉回路

// uva 10054 The Necklace 欧拉回路 // 以颜色为节点,两种颜色互相连着有向边,然后跑一边欧拉回路就ok了 // 这题套了余老师模板书上的欧拉回路,然后就过了 // // 通过这题我了解到了,欧拉回路的基本思想 // 哎,继续练吧... #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat>

UVA 10196 Morning Walk

Problem H Morning Walk Time Limit 3 Seconds Kamalis a Motashotaguy. He has got a new job in Chittagong. So, he has moved to Chittagong fromDinajpur. He was getting fatter in Dinajpur as he had no work in his hand there. So, moving toChittagong has tu

uva 10596 欧拉回路

判断是否存在欧拉回路只要两个条件 图连通,不存在奇度点 注意特判边为0的情况.另外这题数据坑. #include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=208;struct fuck{ int u,v,next;}edge[maxn*maxn];int head[maxn];int tol;void init(){ tol=0; memset(head,

UVA 10196 Morning Walk(欧拉回路)

Problem H Morning Walk Time Limit 3 Seconds Kamalis a Motashotaguy. He has got a new job in Chittagong. So, he has moved to Chittagong fromDinajpur. He was getting fatter in Dinajpur as he had no work in his hand there. So, moving toChittagong has tu