国产探花免费观看_亚洲丰满少妇自慰呻吟_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);	}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临洮县| 平舆县| 镇平县| 台南市| 凤山市| 辉南县| 平罗县| 全南县| 江北区| 恩施市| 来凤县| 凉城县| 杭锦后旗| 建昌县| 体育| 盐边县| 福清市| 富川| 昌乐县| 曲麻莱县| 潮州市| 阿克| 营口市| 南丰县| 贞丰县| 清徐县| 松桃| 红原县| 綦江县| 莱州市| 长春市| 灵川县| 太原市| 桑日县| 新干县| 红安县| 澄城县| 巩留县| 类乌齐县| 宁强县| 金溪县|