首页 > 其他 > 详细

hdu 3018 并查集+顶点度数判定

时间:2014-03-27 00:25:15      阅读:889      评论:0      收藏:0      [点我收藏+]

Ant Trip

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


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.
bubuko.com,布布扣
 

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


一个连通图不会有奇数个奇数度顶点,画一画图可以知道,假如一个连通图所有点度数为偶数,答案为1,否则就是奇数度顶点的一半,累加求和。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/26 20:53:22
File Name :7.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100100;
int du[maxn],fa[maxn],ss[maxn];
int find(int x){
	if(fa[x]!=x)fa[x]=find(fa[x]);
	return fa[x];
}
void bin(int u,int v){
	int t1=find(u),t2=find(v);
	if(t1!=t2)fa[t1]=t2;
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     int n,m;
	 while(~scanf("%d%d",&n,&m)){
		 memset(du,0,sizeof(du));
		 memset(ss,-1,sizeof(ss));
		 for(int i=1;i<=n;i++)fa[i]=i;
		 while(m--){
			 int u,v;
			 scanf("%d%d",&u,&v);
			 du[u]++;du[v]++;
			 bin(u,v);
		 }
		 for(int i=1;i<=n;i++){
			 int t=find(i);
			 if(ss[t]==-1&&du[i]>0)ss[t]=0;
			 if(du[i]&1)ss[t]++;
		 }
		 int ans=0;
		 for(int i=1;i<=n;i++)
			 if(ss[i]>0)ans+=ss[i]/2;
			 else if(ss[i]==0)ans++;
		 cout<<ans<<endl;
	 }
     return 0;
}


hdu 3018 并查集+顶点度数判定,布布扣,bubuko.com

hdu 3018 并查集+顶点度数判定

原文:http://blog.csdn.net/xianxingwuguan1/article/details/22213013

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!