您的位置:首页 > 其它

hdu3018Ant Trip【欧拉道路数量 并查集】

2016-03-16 19:13 344 查看
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.



 

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

 

SampleOutput

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.

 

说题意:给出一个图,问几笔画才能经过所有边

本来应该看到“every road must be visited for exact one time”就想到欧拉道路的==然而更没有想到与并查集有什么关系==

对于一个连通图而言,有这样的一个性质:其需要画的笔数=度数为奇数的点数除以2,那么由于给出的图并没有说明是否是连通图,我们需要用并查集来维护连通图,并且忽略单点的“子图”

/************
hdu3018
2016.3.16
4008 140
C++
1246
************/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int a[100005],deg[100005],odd[100005];
bool used[100005];
int fnd(int x)
{
if(x!=a[x]) a[x]=fnd(a[x]);
return a[x];
}
void addto(int x,int y)
{
x=fnd(x),y=fnd(y);
if(x==y) {
return ;
}
a[y]=a[x];
return ;
}
int main()
{
// freopen("cin.txt","r",stdin);
int n,m,x,y,cnt;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++) {a[i]=i;deg[i]=0;used[i]=false;odd[i]=0;}
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
deg[x]++;deg[y]++;
x=fnd(x);y=fnd(y);
if(x!=y)addto(x,y);
}
vector<int>v;
for(int i=1;i<=n;i++)
{
int f=fnd(i);
if(!used[f])
{
v.push_back(f);
used[f]=1;//!!!
}
if(deg[i]&1)
odd[f]++;
}
cnt=0;
for(int i=0;i<v.size();i++)
{
int k=v[i];
if(deg[k]==0) continue;
if(odd[k]==0) cnt++;
else cnt+=odd[k]/2;
}
printf("%d\n",cnt);
}
return 0;
}

/************
hdu3018
2016.3.16
4008	140
C++
1246
************/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int a[100005],deg[100005],odd[100005];
bool used[100005];
int fnd(int x)
{
if(x!=a[x]) a[x]=fnd(a[x]);
return a[x];
}
void addto(int x,int y)
{
x=fnd(x),y=fnd(y);
if(x==y) {
return ;
}
a[y]=a[x];
return ;
}
int main()
{
// freopen("cin.txt","r",stdin);
int n,m,x,y,cnt;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++) {a[i]=i;deg[i]=0;used[i]=false;odd[i]=0;}
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
deg[x]++;deg[y]++;
x=fnd(x);y=fnd(y);
if(x!=y)addto(x,y);
}
vector<int>v;
for(int i=1;i<=n;i++)
{
int f=fnd(i);
if(!used[f])
{
v.push_back(f);
used[f]=1;//!!!
}
if(deg[i]&1)
odd[f]++;
}
cnt=0;
for(int i=0;i<v.size();i++)
{
int k=v[i];
if(deg[k]==0) continue;
if(odd[k]==0) cnt++;
else cnt+=odd[k]/2;
}
printf("%d\n",cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: