2014
02-27

# Ant Trip

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 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.

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.

3 3
1 2
2 3
1 3

4 2
1 2
3 4

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.



HDU-3018-Ant Trip

http://acm.hdu.edu.cn/showproblem.php?pid=3018

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
int f[100005],hash[100005];
int du[100005],count[100005]; //du为每个节点的次数,count为每个分图奇数次数的点的个数
vector<int> a;
int n,m;
void init()
{
int i;
a.clear();
memset(hash,0,sizeof(hash));
memset(du,0,sizeof(du));
memset(count,0,sizeof(count));
for(i=1;i<=n;i++)
f[i]=i;
}
int find(int x)
{
int r=x;
while(f[r]!=r)
r=f[r];
f[x]=r;
return r;
}
void Union(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
f[fx]=fy;
}
int main()
{
int a1,a2;
int k,i,sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
while(m--)
{
scanf("%d %d",&a1,&a2);
du[a1]++;
du[a2]++;
Union(a1,a2);
}
for(i=1;i<=n;i++)
{
k=find(i);
if(!hash[k])
{
a.push_back(k); //k为一个连通分图的代表元素
hash[k]=1;
}
if(du[i]%2==1)//计算奇数次数的点的个数
count[k]++;
}
sum=0;
for(i=0;i<a.size();i++)
{
k=a[i];
if(du[k]==0) //孤立结点
continue;
if(count[k]==0)//欧拉回路一笔即可画成
sum++;
else
sum+=(count[k]/2);//图中次数为奇数的点一定为偶数个
}
printf("%d\n",sum);
}
return 0;
}

1. 有一点问题。。后面动态规划的程序中
int dp[n+1][W+1];
会报错 提示表达式必须含有常量值。该怎么修改呢。。

2. 算法是程序的灵魂，算法分简单和复杂，如果不搞大数据类，程序员了解一下简单点的算法也是可以的，但是会算法的一定要会编程才行，程序员不一定要会算法，利于自己项目需要的可以简单了解。