The Necklace UVA10054

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;
using std::endl;

const int MAXN(60);

int degree[MAXN];

int is_euler(int mn)
{
	int s = -1;
	for(int i = 1; i <= mn; ++i)
		if(degree[i])
		{
			if(s == -1)
				s = i;
			if(degree[i]%2)
				return -1;
		}
	return s;
}

stack<pair<int, int> > st;
int mp[MAXN][MAXN];

void dfs(int cur, int mn)
{
	for(int i = 1; i <= mn; ++i)
		if(mp[cur][i])
		{
			--mp[cur][i];
			--mp[i][cur];
			dfs(i, mn);
			st.push(make_pair(cur, i));
		}
}

int main()
{
	int T, n_case(0);
	bool println(false);
	scanf("%d", &T);
	while(T--)
	{
		int N;
		scanf("%d", &N);
		int u, v, mn = 1;
		memset(degree, 0, sizeof(degree));
		memset(mp, 0, sizeof(mp));
		for(int i = 0; i < N; ++i)
		{
			scanf("%d%d", &u, &v);
			++degree[u];
			++degree[v];
			++mp[u][v];
			++mp[v][u];
			mn = max(mn, max(u, v));
		}
		int flag = is_euler(mn);
		if(println)
			printf("\n");
		println = true;
		printf("Case #%d\n", ++n_case);
		if(flag == -1)
			printf("some beads may be lost\n");
		else
		{
			dfs(flag, mn);
			while(!st.empty())
			{
				printf("%d %d\n", st.top().first, st.top().second);
				st.pop();
			}
		}
	}
	return 0;
}

你可能感兴趣的:(The Necklace UVA10054)