题目:
一笔画问题
时间限制:
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
-
第一行只有一个正整数N(N<=10)表示测试数据的组数。
题解:
利用并查集判断是否互通,然后根据欧拉图定理判断其结点的度,判断度是否为奇数,如果为奇数其个数只能为0或者2才满足条件
#include<iostream>
#include<cstring>
using namespace std;
int father[5110];
int find(int x)
{
while(x!=father[x])
{
x=father[x];
}
return x;
}
void link(int x,int y)
{
int f1,f2;
f1=find(x);
f2=find(y);
if(f1!=f2)
father[f2]=f1;
}
int main()
{
int N,m,n,x,y,i,degree[5110];
cin>>N;
while(N--)
{
memset(degree,0,sizeof(degree));
cin>>m>>n;
for(i=1;i<=m;i++)
father[i]=i;
int sum=0,count=0;
while(n--)
{
cin>>x>>y;
degree[x]++;
degree[y]++;
link(x,y);
}
for(i=1;i<=m;i++)
{
if(i==father[i])
count++;
if(degree[i]%2==1)
sum++;
}
if((sum==0||sum==2)&&count==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}