您的位置:首页 > 其它

UVA - 10596 Morning Walk

2014-09-15 23:14 387 查看
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 turned to be ablessing for him. Every morning
he takes a walk through the hilly roads ofcharming city Chittagong. He isenjoying this city very much. There are so many roads in Chittagongand every morning he takes different paths for his walking. But while choosinga path he makes sure he does not visit
a road twice not even in his way backhome. An intersection point of a road is not considered as the part of theroad. In a sunny morning, he was thinking about how it would be if he couldvisit all the roads of the city in a single walk. Your task is to help
Kamal in determining whether it is possible for him or not.

 

Input

Input will consist of severaltest cases. Each test case will start with a line containing two numbers. Thefirst number indicates the number of road intersections and is denoted by
N(2 ≤ N ≤ 200). The road intersections are assumed to benumbered from
0 to N-1. The second number R denotes thenumber of roads (0 ≤ R ≤ 10000). Then there will be
Rlines each containing two numbers c1 and
c2indicating the intersections connecting a road.

 

Output

Print a single line containingthe text “Possible” without quotes if it is possible for Kamal to visit all the roads exactly once in a single walkotherwise print “Not Possible”.

 

Sample Input

Output for Sample Input

2 2

0 1

1 0

2 1

0 1

Possible

Not Possible

题意:

给出N个点和R条边,问能不能走完所有的边且每条边只走一次,点可以不走完

欧拉回路

1.连通图;

2.点的入度全部为偶数;

#include <iostream>
#include <cstdio>
#include <cstring>
#define N 300
using namespace std;

int n, m;
int edge

;
int dge
;
int sum;

int judge() {
for (int i = 0; i < n; i++){
if (dge[i] % 2)
return 0;
}
return 1;
}

void euler(int u) {
for (int i = 0; i < n; i++)
if (edge[u][i]) {
edge[u][i]--;
edge[i][u]--;
euler(i);
sum++;
}
}

int main() {
while (scanf("%d%d",&n,&m) != EOF) {
if (m == 0) {
printf("Not Possible\n");
continue;
}
memset(edge,0,sizeof(edge));
memset(dge,0,sizeof(dge));
for (int i = 0; i < m; i++) {
int u,v;
scanf("%d%d",&u,&v);
edge[u][v]++;
edge[v][u]++;
dge[u]++;
dge[v]++;
}

if (judge()) {
sum = 0;
for (int i = 0; i < n; i++) {
if (dge[i]) {
euler(i);
break; //搜索之后就要退出
}
}
if (sum == m)
printf("Possible\n");
else
printf("Not Possible\n");
}
else
printf("Not Possible\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva UVA - 10596 Morning