HDU 3018 Ant Trip (一笔画问题)

                                                   Ant Trip

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.

HDU 3018 Ant Trip (一笔画问题)

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)条路,想要每条道路只走一次,并且全部走完,需要分几次。(需要和朋友分成几个团体,可以正好走完)

思路: 无向图的欧拉路、欧拉回路。 注意  单个城市不考虑 。需要找到共有几条欧拉路 再加上无法构成欧拉路的连通图中度数为奇的节点数/2;

总之     sum = 欧拉路个数 + 连通图中度数为奇的个数/2;

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
vector<int> v;            //利用 vecor 来保存 连通图的个数
const int maxn = 100001;
int f[maxn];
int deg[maxn];
int ji[maxn];
int flag[maxn];

void inti() {
	v.clear();
	for(int i=0; i<maxn; i++) {
		f[i]=i;
		deg[i]=0;
		ji[i]=0;
		flag[i]=0;
	}
}

int find(int r) {        // 并查集来判断是否联通
	while(r!=f[r])
		r=f[r];
	return r;
}



int main() {
	int n,m;

	while(~scanf("%d%d",&n,&m)) {
		inti();
		int a,b;
		int sum = 0;
		for(int i=0; i<m; i++) {
			scanf("%d%d",&a,&b);
			deg[a]++;
			deg[b]++;
			int fa = find(a);
			int fb = find(b);
			f[fb]=fa;
		}

		for(int i =1 ; i<=n; i++) {
			int f = find(i);
			if(!flag[f]) {        // 连通图加入vector
				v.push_back(f);
				flag[f]=1;
			}
			if(deg[i]&1)     // 如果以f为开始的连通图的其他所有节点的度数都为偶,则为欧拉回路
				ji[f]++;      // 否则,将以f为开始的连通图的度数为奇数的节点加 1 
		}

		for(int i = 0; i<v.size(); i++) {

			int k =v[i];
			if(deg[k]==0) continue; // 单个城市则不考虑
			if(ji[k]==0) sum++;
			else sum+=ji[k]/2;

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


	return 0;
}

 

上一篇:《软件工程》-卷积神经网络


下一篇:2476218