hdu 3018 Ant Trip 算是一道欧拉通路的题目吧~

Ant Trip

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1826    Accepted Submission(s): 702

Problem Description

Ant Country consist of N towns.There are M roads connecting the towns.

Ant Tony,together with his friends,wants to go through every part of the country.

They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are trying to divide all the people into several groups,and each may start at different town.Now
tony wants to know what is the least groups of ants that needs to form to achieve their goal.

Input

Input contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating that there are N towns and M roads in Ant Country.Followed by M lines,each
line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town a and town b.No two roads will be the same,and there is no road connecting the same town.

Output

For each test case ,output the least groups that needs to form to achieve their goal.

Sample Input

3 3
1 2
2 3
1 3

4 2
1 2
3 4

Sample Output

1
2

Hint

New ~~~ Notice: if there are no road connecting one town ,tony may forget about the town.
In sample 1,tony and his friends just form one group,they can start at either town 1,2,or 3.
In sample 2,tony and his friends must form two group.

给出N个节点,M个边,问要遍历一遍所有的边,需要的最小group数目。

求一个图中最少几笔画,利用欧拉回路性质,首先得到图的每个强连通分支,然后计算每个强连通分支每个是否都是偶数度是的话,一笔解决,否的话,需要奇数度节点个数的1/2笔解决。(当一个节点度数为奇数时,我们只需要令它的度数为1,因为偶数的话直接抵消了,最后判断一个scc中未1的节点个数,除以2就得到该scc的最小笔画了)

代码:

#include <stdio.h>
#include <string.h>
#define SIZE 100100

int f[SIZE] , deg[SIZE] , ver[SIZE] ;
bool vis[SIZE] ;
int n , m ;
int find(int pos)
{
	int r = pos ;
	while(r != f[r])
		r = f[r] ;
	int temp ;
	while(pos != r)
	{
		temp = f[pos] ;
		f[pos] = r ;
		pos = temp ;
	}
	return r ;
}
void init()
{
	for(int i = 0 ; i <= n ; ++i)
	{
		f[i] = i ;
	}
	memset(deg,0,sizeof(deg)) ;
	memset(ver,0,sizeof(ver)) ;
	memset(vis,false,sizeof(vis)) ;
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		init() ;
		for(int i = 0 ; i < m ; ++i)
		{
			int x, y ;
			scanf("%d%d",&x,&y) ;
			int fx = find(x) , fy = find(y) ;
			deg[x]++ , deg[y]++ ;
			if(fx!=fy)
			{
				f[fx] = fy ;
			}
		}
		bool flag = true ;
		for(int i = 1 ; i<= n ; ++i)
		{
			int fx = find(i) ;
			if(deg[i]%2 == 1)
			{
				ver[fx]++ ;
			}
		}
		int cnt = 0 ;
		for(int i = 1 ; i <= n ; ++i)
		{
			if(deg[i] == 0)
				continue ;
			int fx = find(i) ;
			if(!vis[fx])
			{
				vis[fx] = true ;
				if(!ver[fx])
					cnt ++ ;
				else
					cnt+=ver[fx]/2 ;
			}

		}
		printf("%d\n",cnt) ;
	}
} 

与君共勉

时间: 2024-08-26 02:45:36

hdu 3018 Ant Trip 算是一道欧拉通路的题目吧~的相关文章

[欧拉回路] hdu 3018 Ant Trip

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1658    Accepted Submission(s): 641 Problem Description Ant Country consist of N to

HDU 5883 F - The Best Path 欧拉通路 &amp; 欧拉回路

给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为是回路,所以你的开始点就会被访问多一次,所以如果是欧拉回路的话,还需要O(n)扫一次,枚举每个点作为起点. 欧拉通路的话,结果是固定的,因为只能从奇数度小的那个点作为起点,奇数度大的那个点作为终点. 关于点的访问次数:anstime  = Degree[i] / 2; //如果是奇数的,还要加上一.

HDU 3018 Ant Trip (欧拉路的个数 并查集)

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5501    Accepted Submission(s): 2146 Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,

hdu 3018 Ant Trip 欧拉回路+并查集

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together with his friends,wants to go through every part

hdu 3018 Ant Trip

并查集+欧拉回路 对于每个连通的集合,如果该集合只有一个元素 那么不用管,如果该集合大于一个元素,那么求出奇度的个数,如果奇度个数是0,那么ans+1,否则ans+sum/2,sum为该集合内奇度的个数. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; const int max

关于欧拉通路、欧拉回路的一些定理,推论

关于欧拉通路.欧拉回路的一些定义: 无向图:G是一个连通的无向图(1)经过G的每条边一次并且仅一次的路径为欧拉通路(起点和终点不一定要一样).(2)如果欧拉通路是回路(起点和终点是同一个),则为欧拉回路.(3)具有欧拉回路的无向图G称为欧拉图. 有向图:D是一个有向图,D的基图(把D的有向边改为无向边)是连通的(1)经过D的每条边一次并且仅一次的路径称为有向欧拉通路(起点和终点不一定一样).(2)如果有向欧拉通路是回路(起点和终点一样),那么称为有向欧拉通路.(3)具有有向欧拉通路的有向图D称为

图论——欧拉通路、欧拉回路(有向图无向图混合图)

之前稍微了解有向图.无向图.混合图的欧拉通路.欧拉回路,这里做下笔记,以便日后翻阅. 无向图: 存在欧拉回路的条件:原图连通,每个结点均为偶度结点. 存在欧拉通路的条件:存在欧拉回路,或原图连通,有两个结点为奇度结点,其他结点均为偶度结点. 有向图: 存在欧拉回路的条件:基图连通,每个结点的入度等于出度. 存在欧拉通路的条件:存在欧拉回路,或基图连通,有一个结点入度等于出度+1,有一个结点出度等于入度+1,其他结点的入度等于出度. 混合图: 存在欧拉回路的条件: 1.将无向边随便定向,每个结点的

POJ 1386 Play on Words(有向欧拉通路 连通图)

题意  见下方中文翻译 每一个单词能够看成首尾两个字母相连的一条边  然后就是输入m条边  推断是否能构成有向欧拉通路了 有向图存在欧拉通路的充要条件: 1. 有向图的基图连通: 2. 全部点的出度和入度相等  或者  仅仅有两个入度和出度不相等的点  且这两点入度与出度的差一个为-1(起点)一个为1(终点). 推断是否连通就是应用并查集了 #include<cstdio> #include<cstring> using namespace std; const int N = 3

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

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