HDU 3018 Ant Trip(并查集:无向图欧拉回路的问题)

本文探讨了AntTrip问题,一种经典的图论挑战,旨在寻找最少的蚂蚁群体数量,以便遍历图中所有边恰好一次。文章深入分析了欧拉图和半欧拉图的概念,提出了解决方案,并详细解释了非欧拉图情况下所需笔画次数的计算方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ant Trip

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4709    Accepted Submission(s): 1847

 

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.

http://acm.hdu.edu.cn/../../../data/images/exe3018-1.JPG

 

 

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.

 

 

 

 

Source

2009 Multi-University Training Contest 12 - Host by FZU

 

 

Recommend

Gaojie

题意:(复制饶齐博客)

给你无向图的N个点和M条边,保证这M条边都不同且不会存在同一点的自环边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸)

分析:(思路来自饶齐博客)

可能有多个连通分量,但我们挨个分析每一个连通分量即可:

   1.如果该连通分量是一个孤立的点,显然只需要0.

   2.如果该连通分量是一个欧拉图或半欧拉图,只需要1.

   3.本题的难点:现在关键是连通分量并非一个()欧拉图时,需要几笔?

这里先给出结论:

        一般性的结论是:

       ()欧拉图需要的笔数==该图中奇数度的点数目/2

 

       下面来证明该结论:

 

       首先一个无向图的连通分量中的奇数度的点个数一定是偶数个(成对出现),因为无向图的总度数=偶数.

 

       我们在这种连通分量中每次画一笔有两种选择:

 

       1.a->b->c->d…->g    一条起点与终点不同的路径(路中除首尾度减1,每个点度减2)

 

       2.a->b->c->d…->a    一条起点与终点相同的回路(路中每个点度数减2)

 

       也就是说想要把非()欧拉图分量中的奇数度的点的度数都变成偶数,我们至少需要画奇数度点个数/2 笔(因为我们发现每一笔最多可以消除两个奇度数结点.

 

       那么对于这种图我们最多需要画的笔数 是不是也是 : 奇数度点个数/2 笔呢?

 

       答案是肯定的,这里假设图中有4个奇数度的点,1,2,3,4,5,6.如下图所示:

 

 

       我们先走路:1-> b-> c-> d-> e-> f->a-> 2 这条路.然后6,5 3,4 分别属于两个连通分量了.可以看出只需要3,6/2=3即可.

 

       当然也可以这么画:1->b->2 然后6->f->5 ,接着就剩下了一个半欧拉图了,半欧拉图也是1笔画,正好3.

 

       也就是说对于这种非()欧拉图的连通分量,我们每笔必然消除正好2个点的奇度(使其度变偶数),当最后一笔的时候我们必然消除所有的点的度数(包括剩下的两个奇点,因为最后一笔就必然是欧拉通路).但是有一点要注意,有可能过程中的某几笔会使得该连通分量变成多个连通分量,当然结论不变.

 

       经过上面的分析,这题的结论出来了,对于每个以i为根的连通分量我们记录属于该连通分量的点数目num[i]和该连通分量中奇度点的个数odd[i].

 

       如果num[i]==01,0.(注意num[i]==0表示i点不是根,num[i]==1表示i点是一个孤立的点.)

 

       如果num[i]>1odd[i]==0 1

 

       如果num[i]>1odd[i]>0 odd[i]/2

 

#include <cstdio>
#include <cstring>
#define N 100010
 
using namespace std;
 
int n, m;
int f[N],degree[N],odd[N],num[N];//记录第i点的度数
 
void init()
{
	for (int i = 1; i <= n; i++)
		f[i] = i;
}
int find(int x)
{
	return x == f[x] ? x : f[x] = find(f[x]);
}
void merge(int x, int y)
{
	int t1, t2;
	t1 = find(x); t2 = find(y);
	if (t1 != t2)	f[t2] = t1;
	else return;
}
int main()
{
	while (scanf("%d", &n) != EOF && n)
	{
		init();
		memset(degree, 0, sizeof(degree));
		memset(num, 0, sizeof(num));
		memset(odd, 0, sizeof(odd));
		scanf("%d", &m);
		int t1, t2;
		for (int i = 0; i < m; i++)
		{
			scanf("%d%d", &t1, &t2);
			//输入有t1,t2相等的情况
			if (t1 == t2)
				continue;
			degree[t1]++; degree[t2]++;
			merge(t1, t2);
		}
		for(int i=1;i<=n;i++)
        {
            num[find(i)]++;      //num[i]=x表以i为根的连通分量中有x个节点
            if(degree[i]&1) odd[find(i)]++;//odd[i]=x表以i为根的连通分量中有x个奇度的点
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(num[i]<=1) continue;
            else if(odd[i]==0) ans++;
            else if(odd[i]>0) ans+=odd[i]/2;///欧拉通路也是需要一条的
        }
        printf("%d\n",ans);
		//printf("%d\n", isEuler() && isconnect());
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值