一笔画问题 南阳oj42 【并查集+欧拉通路的判断】

描述

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

无向图是否具有欧拉通路或回路的判定:

欧拉通路:图连通;图中只有0个或2个度为奇数的节点

欧拉回路:图连通;图中所有节点度均为偶数

#include<stdio.h>
#include<string.h>
int n;
int per[1050];
int num[1050];
void init()
{
	for(int i=1;i<=n;++i)
	{
		per[i]=i;
	}
}
int find(int x)
{
	int r=x;
	while(r!=per[r])
	{
		r=per[r];
	}
	int i=x,j;
	while(i!=r)
	{
		j=per[i];
		per[i]=r;
		i=j;
	}
	return r;
}
void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
	}
}
int main()
{
	int i, j;
	int m;
	int T;
	int x, y;
	scanf("%d",&T);
	while(T--)
	{
		memset(num,0,sizeof(num));
		scanf("%d%d",&n,&m);
		if(n==1)
		{
			printf("Yes\n");
			continue;
		}
		init();
		for(i=0;i<m;++i)
		{
			scanf("%d%d",&x,&y);
			num[x]++;num[y]++;
			join(x,y);
		}
		bool flag=0;
		int cnt1=0,cnt2=0;
		for(i=1;i<=n;++i)
		{
			if(num[i]&1) cnt1++;		//奇数度有几个
			if(per[i]==i) cnt2++;		//有几个根节点
		}

		if((cnt1==0||cnt1==2)&&cnt2==1) printf("Yes\n");
		else printf("No\n");
	}

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-13 22:40:36

一笔画问题 南阳oj42 【并查集+欧拉通路的判断】的相关文章

FZU2112 Tickets (并查集+欧拉通路)经典

Problem 2112 Tickets Accept: 309    Submit: 526 Time Limit: 3000 mSec    Memory Limit : 32768 KB Problem Description You have won a collection of tickets on luxury cruisers. Each ticket can be used only once, but can be used in either direction betwe

POJ 2513 无向欧拉通路+字典树+并查集

题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧拉通路的判断,但是map效率太低,超时了 网上看了一遍发现必须得用效率更高的字典树对每个不同的颜色进行赋值 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace st

poj 2513 Colored Sticks(欧拉通路+并查集+字典树)

题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色,现在给定每个木棍两端的颜色,不同木棍之间拼接需要颜色相同的 端才可以,问最后能否将N个木棍拼接在一起. 解题思路:欧拉通路+并查集+字典树.欧拉通路,每个节点的统计度,度为奇数的点不能超过2个.并查集,判断节点 是否完全联通.字典树,映射颜色. #include <cstdio> #include <cstring> #include <string> #includ

poj2513Colored Sticks(欧拉通路+字典树+并查集)

Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color? Input Input is a

POJ-2513 Colored Sticks(字典树+并查集+欧拉)

题目链接:Colored Sticks 一道3个知识点结合的题目,可以说单个知识点的题目,都会做,一旦知识点结合起来,题目就不简单了 思路:这个题开始看就知道是并查集,但是不好处理的不同种单词的统计,所以理所应当联想到字典树,上次做字典树的题目啸爷出的是统计相同单词数,这个题目和那个一样,把flag加个编号即可,再利用并查集. 1750ms  水过 #include <iostream> #include <cstdio> #include <cstdlib> #inc

hdu 3018Ant Trip(一笔画问题,用并查集就无向图的连通分量)

1.题意:给一个无向简单图,问至少几笔画画完所有的边. 2.思路:①先用并查集求出有几个连通分量:②如果连通分量中只有一个结点,那么就是0笔画:③在一个简单无向连通图中,如果没有欧拉回路,至少要用n/2笔画画完所有边,n是奇点个数. 3AC代码一(93ms): #include<cstdio> #include<cstring> #include<set> #include<vector> using namespace std; int n,m; int

HDU5883 The Best Path(并查集+欧拉路)

题意: n个点m条边,问m条边构成的是否为欧拉路. 是的话输出路径上所有点的异或和,每个点经过几次异或几次. 思路: 先用并查集判断是否连通,然后如果是欧拉路的话有两种情况 如果奇数度节点有2个,就枚举这两个点做起点,选大的 如果都为偶数度节点,就枚举n个起点,选大的 /* *********************************************** Author :devil ************************************************ *

EOJ 1816 并查集的的简单应用——判断图联通

题目:eoj1816 题目分析:判断图是否连通,可用dfs遍历图算法.这里我采用的是并查集的方法.初始化时将每个节点看作一个集合,则每给出一条边即把两个集合合并.最后遍历所有点,有几个集合便有几个连通分量,若只有一个集合说明图连通. AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string> #include &

POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 35612   Accepted: 9324 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st