P - Ant Trip
来源:互联网 发布:烟台海颐软件 编辑:程序博客网 时间:2024/06/05 13:28
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.
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 31 22 31 34 21 23 4
Sample Output
12
题意:n个村子间有m条路,每条路只能走一次,问最少需要多少队人能走完所有的路
解法:典型的欧拉图,先判断图中有多少联通分量,再判断每个联通分量度为奇数的点的个数,则每个联通分量所需的队伍数量为1或者为奇数点的个数/2中的最大值
<span style="font-size:18px;">#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<queue>#include<vector>using namespace std;vector<int>edge[100010];int dis[100010],vis[100010],odd[100010];//dis记录每个点的联通分量,vis记录每个点出现的次数,记录每个联通分量里度数为奇数的个数int main(){ int m,n,i; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) { edge[i].clear(); dis[i]=0; vis[i]=0; odd[i]=0; } for(i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); vis[a]++; vis[b]++; edge[a].push_back(b);//注意,这里两边都要进表,因为如果不这样,若a>b就要出问题 edge[b].push_back(a); } queue<int>q; int k=0; for(i=1;i<=n;i++) { if(dis[i]==0) { if(vis[i]==0)//孤立的点不管 continue; dis[i]=++k; } else continue; q.push(i); while(!q.empty()) { int num=q.front(); q.pop(); for(int j=0;j<edge[num].size();j++)//遍历在一条联通分量上的点 { int val=edge[num][j]; if(dis[val]==0)//此点还未标记联通分量 { dis[val]=k; q.push(val); } } } } for(i=1;i<=n;i++) { if(vis[i]%2==1) odd[dis[i]]++;//记录各联通分量的度为奇数点的个数 } int ans=0; for(i=1;i<=k;i++) { ans+=max(1,odd[i]/2);//求每天联通分量需要的队伍数量 } printf("%d\n",ans); } return 0;}</span>
0 0
- P - Ant Trip
- Ant Trip
- Ant Trip
- Ant Trip
- hdu 3018 Ant Trip
- HDU-3018-Ant Trip
- hdu 3018 Ant Trip
- hdu 3018 Ant Trip
- hdu 3018 Ant Trip
- hdu3018 Ant Trip
- HDU 3018 Ant Trip
- HDU 3018 Ant Trip
- Hdu 3018 Ant Trip
- hdu3018 Ant Trip
- HDU 3018 Ant Trip
- HDU 3018 Ant Trip
- [hdu3018]Ant Trip欧拉路
- hdu3018 Ant Trip 欧拉路
- 如何解决JavaScript中UUID作为方法参数在方法中无法传递而数字却正常传递的问题
- A - 还是畅通工程
- Nginx-Rtmp-Module命令
- npm的一些特殊使用
- js中几种实用的跨域方法原理详解
- P - Ant Trip
- static修饰变量只占一份内存空间与泛型
- git linux上自动化打包发布脚本
- 4. Median of Two Sorted Arrays
- ld: library not found for -l+第三方库名称报错分析
- 生活健康的9个标准
- H5:前端开发性能优化
- 勿忘初心,继续coding
- 碰撞的方向判定,和坐标转换