FZU 2112(欧拉通路&&判联通图)

Problem 2112 Tickets

Accept: 447    Submit: 770
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 between the 2 different cities printed on the ticket. Your prize gives you free airfare to any city to start your cruising, and free airfare back home from wherever you finish your cruising.

You love to sail and don't want to waste any of your free tickets. How many additional tickets would you have to buy so that your cruise can use all of your tickets?

Now giving the free tickets you have won. Please compute the smallest number of additional tickets that can be purchased to allow you to use all of your free tickets.

 Input

There is one integer T (T≤100) in the first line of the input.

Then T cases, for any case, the first line contains 2 integers n, m (1≤n, m≤100,000). n indicates the identifier of the cities are between 1 and n, inclusive. m indicates the tickets you have won.

Then following m lines, each line contains two integers u and v (1≤u, v≤n), indicates the 2 cities printed on your tickets, respectively.

 Output

For each test case, output an integer in a single line, indicates the smallest number of additional tickets you need to buy.

 Sample Input

35 31 31 24 56 51 31 21 61 51 43 21 21 2

 Sample Output

120

 Source

“高教社杯”第三届福建省大学生程序设计竞赛




题解:这一题的难点在于判断图的每个联通分块里有多少条路构成不了欧拉路,那么需要根据欧拉路的性质可知,对于无向图,构成欧拉路的奇数点的个数为0或者为2,那么我们只要统计出这个联通分块的(奇数点的个数-2)就是多余出来的那些点,那么直接累加(奇数点的个数-2)/2即可,代码里有更详细的说明。


这里我们使用并查集判断联通块即可







#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;

#define N int(1e5+10)  
#define inf int(0x3f3f3f3f)  
#define mod int(1e9+7)  
typedef long long LL;

int fa[N], degree[N],odd[N];
void init(int n)
{
	for (int i = 1; i <= n; i++)
	{
		fa[i] = i;
		degree[i] = 0;
		odd[i] = 0;
	}
}

int find(int x)
{
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void unio(int x, int y)
{
	x = find(x);
	y = find(y);
	if (x != y)fa[x] = y;
}

int main()
{
#ifdef CDZSC  
	freopen("i.txt", "r", stdin);
	freopen("o.txt", "w", stdout);
	int _time_jc = clock();
#endif  

	int t, x, y, n, m;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &m);
		init(n);
		while (m--)
		{
			scanf("%d%d", &x, &y);
			degree[x]++;
			degree[y]++;//统计点的出现次数
			unio(x, y);
		}
		int ans = -1;
		for (int i = 1; i <= n; i++)
		{
			if (degree[i])//如果存在这个点
			{
				if (fa[i] == i)ans++;//每个联通图贡献一个邮票
				if (degree[i] & 1)
				{
					odd[find(i)]++;
					/*使用并查集将所有的点放入这个联通
					块的祖先,统计这个联通块有多少个奇数点
					*/
				}
			}
		}
		for (int i = 1; i <= n; i++)
		{
			ans += max(0, (odd[i] - 2) / 2);
			/*每个联通图有0或2个奇数点,直接将每个
			联通图的奇数点的个数-2,去除多余的点,剩
			下的奇数点构成不了欧拉图,(就需要买邮票)由图论的性质可知
			奇数点的个数必定是偶数(头尾),那么直接除2即可
			*/
		}
		printf("%d\n", ans);
	}
	return 0;
}                   









你可能感兴趣的:(FZU 2112(欧拉通路&&判联通图))