One-stroke painting problem (and check the set)

One-stroke drawing problem
Time limit: 3000 ms | Memory limit: 65535 KB
Difficulty: 4
Description
zyc likes to play some small games since he was a child, including drawing a one-stroke drawing. He wants to ask you to help him write a program to determine whether a picture can be Draw with a single stroke.

It is stipulated that all edges can only be drawn once and cannot be drawn repeatedly.


The first line of input has only one positive integer N (N<=10) representing the number of groups of test data.
The first line of each set of test data has two positive integers P, Q (P<=1000, Q<=2000), which respectively indicate how many vertices and how many lines there are in the painting. (points are numbered from 1 to P)
subsequent Q lines, each with two positive integers A, B (0

/*
    判断一笔画的问题
    首先图必须保证是联通的
    第二是 如果存在一个欧拉路的话  度数为奇数个的点必须为0 或者 2
    如果存在一个欧拉回路的话 所有的度数必须是偶数
    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=42
*/

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int du[1007];
int pre[1007];
int vis[1007][1007];//判断边是否重复
int getF(int x)
{
    if(x != pre[x]) return pre[x] = getF(pre[x]);
    return x;
}
void Union(int x,int y)
{
    int pa = getF(x);
    int pb = getF(y);
    if(pa != pb)
    {
        pre[pa] = pb;
    }
}
int main()
{
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
        memset(vis,0,sizeof(vis));
        memset(du,0,sizeof(vis));
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i =1;i<= n;i++)
            pre[i] = i;
        for(int i = 0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(!vis[a][b])//消掉重复边
            {
                vis[a][b] = 1;
                du[a]++;
                du[b]++;
                Union(a,b);
            }
        }
        int cnt =0;
        int flag = 0;
        for(int i = 1;i<=n;i++)
        {
            int f = getF(i);
            if(f == i) flag ++;
            if(du[i] % 2 ) cnt++;
        }
        if(flag >= 2)
        {
            printf("No\n");
        }
        else
        {
            if(flag==0)//存在一个回路
            {
                if(cnt == 0)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
            else // 不存在回路
            {
                if(cnt == 0 || cnt == 2)
                {
                    printf("Yes\n");
                }
                else
                    printf("No\n");
            }
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324682975&siteId=291194637