您的位置:首页 > 其它

hdu acm 3018 Ant Trip

2016-01-26 16:56 288 查看
[align=left]Problem Description[/align]
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.



 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each test case ,output the least groups that needs to form to achieve their goal.

 

[align=left]Sample Input[/align]

3 3
1 2
2 3
1 3

4 2
1 2
3 4

 

[align=left]Sample Output[/align]

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.

 
欧拉路问题。。。

#include <iostream>
using namespace std;
#include<string.h>
#include<algorithm>
#include<vector>
#include<stdio.h>
#define maxn 100005
int bin[maxn];
int vis[maxn],n,m;
int findx(int x)
{
if(x!=bin[x])
return bin[x]=findx(bin[x]);
return bin[x];
}
void fun(int x,int y)
{
int tx,ty;
tx=findx(x);
ty=findx(y);
if(tx!=ty)
bin[tx]=ty;
}
int main()
{ //freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);
int i,a,b,deg[maxn],ans,v[maxn],g,num[maxn];
while(cin>>n>>m)
{
memset(deg,0,sizeof(deg));
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
for(i=0;i<=n;i++)
bin[i]=i;
for(i=0;i<m;i++)
{
cin>>a>>b;
fun(a,b);
deg[a]++,deg[b]++;
}
memset(v,0,sizeof(v));
int j=0;
for(i=1;i<=n;i++)
{
g=findx(i);
if(!vis[g])
{
v[j++]=g;
vis[g]=1;
}
if(deg[i]%2==1)
num[g]++;
}
ans=0;
for(i=0;i<j;i++)//有j个连通块。
{
int k=v[i];
//cout<<k<<" "<<deg[k]<<" "<<num[k]<<endl;
if(!deg[k])continue;//如果度为0,说明它是一个孤立的点,忽略掉。
if(deg[k]&&num[k]==0)ans++;//如果度不为0并且没有奇数度的顶点,构成欧拉回路,只需要1组
else ans=ans+num[k]/2;//如果度不为0且存在奇数度的顶点,则ans=奇数度的顶点/2组
}
cout<<ans<<endl;
}
// fclose(stdin);fclose(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: