图论基础UVA10054

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int maxn=55,maxm=1010;
int a[maxn][maxn],num[maxn],ans[maxm];
int n,m;
void dfs(int x)
{
	for(int i=1;i<=50;i++)
	{
		if(a[x][i])
		{
			a[x][i]--;
			a[i][x]--;
			dfs(i);
		}
	}
	ans[++m]=x;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("uva10054.in","r",stdin);
//	freopen("uva10054.out","w",stdout);	
#endif
	int i,j,k,T;
	int x,y,u,flag;
	scanf("%d",&T);
	for(u=1;u<=T;u++){
		if(u>1)printf("\n");
		printf("Case #%d\n",u);
		memset(a,0,sizeof(a));
		memset(num,0,sizeof(num));
		flag=1;
		m=0;
		scanf("%d",&n);
		for(i=1;i<=n;i++){
			scanf("%d%d",&x,&y);
			a[x][y]++;
			a[y][x]++;
			num[x]++;
			num[y]++;
		}
		int p;
		for(i=1;i<=50;i++){
			if(num[i]%2!=0){
				flag=0;
				break;
			}
			if(num[i])p=i;
		}
		if(flag)dfs(p);
		if(!flag || m!=n+1)
			printf("some beads may be lost\n");
		else
			for(i=1;i<m;i++)
				printf("%d %d\n",ans[i],ans[i+1]);			
	}	
	return 0;
}

你可能感兴趣的:(图论基础UVA10054)