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 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 number
R denotes the number of roads (0 ≤ R ≤ 10000). Then there will beRlines each containing two numbers
c1 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;
}

UVA 10196 Morning Walk

时间: 2024-10-13 01:46:50

UVA 10196 Morning Walk的相关文章

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 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 - 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 10196 - Check The Check

题目:国际象棋,判断当前状态,哪一方被将军了.不会有同时被将军的情况. 分析:模拟.直接按照国际象棋的规则模拟即可. 把操作分成两种,单点判断和射线判断,写成函数减少公共代码,也降低错误率: 然后:兵.马.王(不用判断)都是单点判断,后.车.象都是射线判断. 每种情况,调用不同的方向向量即可. 说明:没有同时成立的情况,注意细节别写错就好了:还有最后那组别输出了. #include <iostream> #include <cstdlib> #include <cstdio&

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

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 10917 A Walk Through the Forest

A Walk Through the Forest Time Limit:1000MS  Memory Limit:65536K Total Submit:48 Accepted:15 Description Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes t

UVa 10596 Moring Walk【欧拉回路】

题意:给出n个点,m条路,问能否走完m条路. 自己做的时候= =三下两下用并查集做了交,WA了一发-后来又WA了好几发--(而且也是判断了连通性的啊) 搜了题解= = 发现是这样的: 因为只要求走完所有的路,即为只需要走完已经给出的路,而并没有要求所走得路上含有所有的点, 比如说 给出的路有这些 0 1 1 2 2 3 3 0 4 4 那么构成的路即为,绕着图中的蓝色线走一圈,即为走完了所有的路, 而4是一个孤立点,也并没有构成路,所以不需要管它 代码中的 if(d[i]!=0)是判断这个点是否

uva 10596 Morning Walk(欧拉回路)

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