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;
}
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;
}
相关文章推荐
- 详解c++ 引用(reference)与 指针(pointer)的区别与联系
- 硬盘接口类型
- Win8快速开机到底隐藏了什么秘密
- Python 连接 Hbase
- 232,下载图片到沙盒的整个流程
- MATLAB.NET混合编程之二~~由C#调用MATLAB编译生成的dll
- 证明01分数背包问题具有贪心选择性质
- [转]nodejs npm常用命令
- Win7 桌面应用图标不见了
- css 权重 特殊性
- HUST 1027 Enemy Target!
- 高可用可伸缩架构实用经验谈
- 函数参数之 传常引用(passed by reference to const)替换 传值(passed by value)
- 作用域
- C# 微信公众平台开发(5)--添加图文素材
- MyEclipse 8.5整合Git,并在Github上发布项目【转】
- 回流与重绘
- ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
- SpringMVC深度探险(四) —— SpringMVC核心配置文件详解
- 外层竖向ScrollView,里层横向ScrollView滑动冲突