hdu 3018 Ant Trip (欧拉图+并查集)
来源:互联网 发布:传世引擎源码 编辑:程序博客网 时间:2024/05/18 20:33
Ant Trip
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2727 Accepted Submission(s): 1075
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.
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
12HintNew ~~~ 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
2009 Multi-University Training Contest 12 - Host by FZU
Recommend
gaojie | We have carefully selected several similar problems for you: 3013 3015 3016 3011 3010
题解:欧拉图
用并查集维护连通性。对于同一个连通块中的点,如果所有点的度都是偶数,那么该连通块中存在欧拉回路。如果有K个奇数度的结点,那么需要k/2条路径来覆盖。
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<cmath>#define N 500003using namespace std;int n,m,tot,nxt[N],point[N],v[N],cnt,sz,x[N],y[N],ans,fa[N];int dfsn[N],low[N],st[N],top,ins[N],belong[N],du[N],size[N];struct data{int bl,x;}a[N];int cmp(data a,data b){return a.bl<b.bl;}int find(int x){if (fa[x]==x) return x;fa[x]=find(fa[x]);return fa[x];}int main(){freopen("a.in","r",stdin);freopen("my.out","w",stdout);while (scanf("%d%d",&n,&m)!=EOF) {memset(du,0,sizeof(du));tot=0; ans=0;for (int i=1;i<=n;i++) fa[i]=i;for (int i=1;i<=m;i++) { scanf("%d%d",&x[i],&y[i]);int r1=find(x[i]); int r2=find(y[i]);if (r2!=r1) fa[r2]=r1;du[x[i]]++; du[y[i]]++;}for (int i=1;i<=n;i++) fa[i]=find(i),a[i].x=i,a[i].bl=fa[i];sort(a+1,a+n+1,cmp);int k=0; int s=0;//for (int i=1;i<=n;i++) cout<<du[i]<<" ";//cout<<endl;for (int i=1;i<=n;i++) if (a[i].bl!=a[i-1].bl||i==n) { if (i==n&&a[i].bl==a[i-1].bl) { s++; if (du[a[i].x]&1) k++; } //cout<<k<<" "<<s<<" "<<ans<<endl; if (k) ans+=k/2; else if (s>1) ans++; k=0; s=1; if (du[a[i].x]&1) k=1; }else { if (du[a[i].x]&1) k++; s++; }printf("%d\n",ans);}}
0 0
- hdu 3018 Ant Trip (欧拉图+并查集)
- HDU 3018 Ant Trip 【欧拉图+并查集】
- HDU 3018 Ant Trip【欧拉路、并查集】
- HDU 3018 Ant Trip ( 并查集+欧拉回路 )
- HDU 3018 Ant Trip 欧拉路 并查集
- HDU 3018-Ant Trip(并查集&&欧拉)
- HDU 3018 Ant Trip(欧拉路径 + 并查集 + 连通性判断)
- hdu-3018-Ant Trip(并查集&&欧拉回路)
- [HDU1038]Ant Trip(并查集+欧拉图)
- hdu 3018 Ant Trip
- HDU-3018-Ant Trip
- hdu 3018 Ant Trip
- hdu 3018 Ant Trip
- hdu 3018 Ant Trip
- HDU 3018 Ant Trip
- HDU 3018 Ant Trip
- Hdu 3018 Ant Trip
- HDU 3018 Ant Trip
- spring创建bean模式singleton与prototype的区别
- Android应用组件之Service
- python-re模块常用的函数及方法
- Java动态代理整理笔记
- 百万以内素数快速查找
- hdu 3018 Ant Trip (欧拉图+并查集)
- 预计工业物联网市场到2021年达到1238亿美元
- 比特币哈希函数简述
- Linux驱动配置文件选项的修改
- javascript作用域、链/闭包的理解
- 用户管理的 CURD 应用 (react+dva+antd)
- Linux的hostname详细说明
- LSTM相关的Python代码
- Android性能测试工具Emmagee