NYOJ 一笔画问题 欧拉路

一笔画问题

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
 
描述

zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来。

规定,所有的边都只能画一次,不能重复画。

 

 
输入
第一行只有一个正整数N(N<=10)表示测试数据的组数。
每组测试数据的第一行有两个正整数P,Q(P<=1000,Q<=2000),分别表示这个画中有多少个顶点和多少条连线。(点的编号从1到P)
随后的Q行,每行有两个正整数A,B(0<A,B<P),表示编号为A和B的两点之间有连线。
输出
如果存在符合条件的连线,则输出"Yes",
如果不存在符合条件的连线,输出"No"。
样例输入
2
4 3
1 2
1 3
1 4
4 5
1 2
2 3
1 3
1 4
3 4
样例输出
No
Yes 

判断欧拉路是否存在的方法

有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。

无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN  1003
#define LLL 1000000000
#define INF 1000000009
/*
DFS能搜到所有的点
*/
vector<int> E[MAXN];
int pre[MAXN],n,m;
int find(int x)
{
    if (pre[x] == -1)
        return x;
    else
        return pre[x] = find(pre[x]);
}
void mix(int x, int y)
{
    int fx = find(x), fy = find(y);
    if (fx != fy)
    {
        pre[fy] = fx;
    }
}
int main()
{
    int T;
    
        memset(pre, -1, sizeof(pre));
        scanf("%d%d", &n, &m);
        int f, t;
        for (int i = 1; i <= n; i++)
            E[i].clear();
        while (m--)
        {
            scanf("%d%d", &f, &t);
            mix(f, t);
            E[f].push_back(t);
            E[t].push_back(f);
        }
        int cnt = 0,num=0;
        for (int i = 1; i <= n; i++)
        {
            if (E[i].size() % 2)
                cnt++;
            if (find(i) == i)
                num++;
        }
        if (cnt == 2 && num == 1)
            printf("Yes\n");
        else
            printf("No\n");
    return 0;
}

 

posted @ 2017-05-09 21:20  joeylee97  阅读(245)  评论(0编辑  收藏  举报