国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

hdu 3018 Ant Trip (歐拉圖+并查集)

2019-11-08 01:58:39
字體:
來源:轉載
供稿:網友

Ant Trip

Time Limit: 2000/1000 MS (java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2727    Accepted Submission(s): 1075PRoblem DescriptionAnt 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.
 InputInput 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. OutputFor 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.  Source2009 Multi-University Training Contest 12 - Host by FZU Recommendgaojie   |   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);	}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嵊州市| 休宁县| 霍邱县| 库伦旗| 高唐县| 元氏县| 辽阳县| 探索| 闽清县| 康定县| 汝城县| 内江市| 广南县| 宝兴县| 巧家县| 通江县| 广饶县| 出国| 洛阳市| 太仓市| 公主岭市| 横山县| 合肥市| 台南市| 古田县| 华蓥市| 惠水县| 扎赉特旗| 冕宁县| 华阴市| 广河县| 南安市| 东港市| 清徐县| 甘肃省| 栾川县| 眉山市| 杂多县| 巨鹿县| 金湖县| 根河市|