您的位置:首页 > 其它

UVA 10196 Morning Walk(欧拉回路)

2014-07-17 12:43 176 查看
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 a blessing for him. Every morning
he takes a walk through the hilly roads of charming city Chittagong. He is enjoying this city very much. There are so many roads in Chittagongand every morning he takes different paths for his walking. But while choosing a path he makes sure he does not visit
a road twice not even in his way back home. An intersection point of a road is not considered as the part of the road. In a sunny morning, he was thinking about how it would be if he could visit 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 several test cases. Each test case will start with a line containing two numbers. The first number indicates the number of road intersections and is denoted byN(2 ≤ N ≤ 200). The road intersections
are assumed to be numbered from0 to N-1. The second numberR denotes the number of roads (0 ≤ R ≤ 10000). Then there will beRlines each containing two numbersc1
andc2indicating the intersections connecting a road.

Output

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

Sample Input

Output for Sample Input

2 2

0 1

1 0

2 1

0 1

Possible

Not Possible

WA了一个上午,终于找出错误了,visit all the roads of the city in a single walk.走完所有的边且每条边只走一次,点可以不走完,一直WA在这里。

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

deg记录每个点的度,并查集判断所有的边和这些边连接的点是不是一个连通图。

#include<stdio.h>
#include<string.h>
int father[205], deg[205];

int Find(int x)
{
    if(x != father[x])
        father[x] = Find(father[x]);
    return father[x];
}

void Init(int n)
{
    memset(deg, 0, sizeof(deg));
    for(int i = 0; i < n; i++)
        father[i] = i;
}

int main()
{
    int n, r,i;
    while(scanf("%d%d",&n,&r)!=EOF)
    {
        if(r == 0)
        {
            printf("Not Possible\n");
            continue;
        }
        Init(n);
        int u, v;
        for(i = 0; i < r ; i++)
        {
            scanf("%d%d",&u,&v);
            father[Find(u)] = Find(v);
            deg[u]++;
            deg[v]++;
        }
        int ok = 1;
        int j = 0;
        while(deg[j] == 0)
            j++;
        for(i = j + 1; i < n; i++)
            if(deg[i] && Find(i) != Find(j))
            {
                ok = 0;
                break;
            }
        int cnt = 0;
        for(i = 0; i < n; i++)
            if(deg[i] % 2 != 0)
                cnt++;
        if(!ok || cnt > 0)
            printf("Not Possible\n");
        else
            printf("Possible\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: