Ant Trip
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1047 Accepted Submission(s): 389
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/87f4654e3e9f30d385bf389cf2483a1a.jpeg)
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 2HintNew ~~~ 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.
Source
Recommend
gaojie
题意:求走遍所有的路,每条路只走一次至少需要几个人
分析:用dfs求出连通分图,对于每个连通分图,求奇点的个数,每两个奇点需要一个人走,特别的,当图中没有奇点的时候也需要一个人,需要注意的是没有边的点不需要考虑
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#define N 100005
using namespace std;
int ans,vis[N],cnt,k,d[N],f[N];
vector<int> v[N],edge[N];
void dfs(int x)
{
int i;
vis[x]=1;
v[cnt].push_back(x); //将每一个连通分图的边存起来
for(i=0;i<edge[x].size();i++)
{
if(!vis[edge[x][i]])
dfs(edge[x][i]);
}
}
int main()
{
int x,y,n,m,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=0;
memset(d,0,sizeof(d));
memset(f,0,sizeof(f));
for(i=0;i<n;i++)
{
edge[i].clear();
v[i].clear();
}
while(m--)
{
scanf("%d%d",&x,&y);
edge[x].push_back(y);
edge[y].push_back(x);
d[x]++;
d[y]++;
f[x]=f[y]=1; //标记点之间有边
}
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
if(f[i]&&!vis[i])
{
// cout<<i<<endl;
dfs(i);
cnt++;
}
}
ans=0;
// cout<<cnt<<endl;
for(i=0;i<cnt;i++)
{
k=0;
for(j=0;j<v[i].size();j++)
{
//cout<<v[i][j]<<endl;
if(d[v[i][j]]%2)
k++;
}
// cout<<k<<endl;
k=k/2; // 每2个奇点可以走一次欧拉路
if(k==0) //图为欧拉图的时候需要一个人走
k++;
ans+=k;
}
printf("%d\n",ans);
}
return 0;
}