现在的位置: 首页 > 综合 > 正文

SGU 101. Domino

2019年02月15日 ⁄ 综合 ⁄ 共 3156字 ⁄ 字号 评论关闭


101. Domino

time limit per test: 0.25 sec.
memory limit per test: 4096 KB

Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards.
The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...

The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

ENCYCLOPÆDIA BRITANNICA

Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

Input

The first line of the input contains a single integer N (1 ≤
N ≤ 100) representing the total number of pieces in the domino set. The following
N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

Output

Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first
means that you not rotate that piece, and second if you rotate it).

Sample Input

5
1 2
2 4
2 4
6 4
2 1

Sample Output

2 -
5 +
1 +
3 +
4 -

无向图欧拉通路

/*************************************************************************
    > File Name: sgu101.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年01月29日 星期四 15时29分38秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 10;
int father[N];
int head[N], tot, cnt;
bool vis[N];
int deg[N];

struct dio
{
	int u, v;
}dmo[110];

struct ord
{
	bool flag;
	int id;
}st[110 * 110];

struct node
{
	int next;
	int to;
	bool flag;
	int id;
}edge[100 * 100];

void addedge (int from, int to,int id)
{
	edge[tot].id = id;
	edge[tot].flag = false;
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

int find (int x)
{
	if (father[x] == -1)
	{
		return x;
	}
	return father[x] = find (father[x]);
}

void dfs (int u,int id)
{
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		if (edge[i].id == id || edge[i].flag)
		{
			continue;
		}
		int v = edge[i].to;
		edge[i].flag = 1;
		edge[i ^ 1].flag = 1;
		dfs (v, edge[i].id);
		st[++cnt].id = edge[i].id;
		if (dmo[edge[i].id].u == u)
		{
			st[cnt].flag = 1;
		}
		else
		{
			st[cnt].flag = 0;
		}
	}
}

int main()
{
	int n;
	while (~scanf("%d", &n))
	{
		memset (head, -1, sizeof(head));
		memset (father, -1, sizeof(father));
		memset (vis, 0, sizeof(vis));
		memset (deg, 0, sizeof(deg));
		cnt = tot = 0;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d", &dmo[i].u, &dmo[i].v);
			addedge (dmo[i].u, dmo[i].v, i);
			addedge (dmo[i].v, dmo[i].u, i);
			int u = find (dmo[i].u);
			int v = find (dmo[i].v);
			++deg[dmo[i].u];
			++deg[dmo[i].v];
			vis[dmo[i].u] = vis[dmo[i].v] = 1;
			if (u != v)
			{
				father[u] = v;
			}
		}
		int ret = 0, x = 0;
		int s = -1;
		for (int i = 0; i < N; ++i)
		{
			if (vis[i] && father[i] == -1 && deg[i])
			{
				++ret;
			}
			if (vis[i] && deg[i] % 2)
			{
				++x;
				s = i;
			}
		}
		if (ret > 1 || !(x == 0 || x == 2))
		{
			printf("No solution\n");
			continue;
		}
		if (s == -1)
		{
			for (int i = 0; i < N; ++i)
			{
				if (vis[i])
				{
					s = i;
					break;
				}
			}
		}
		dfs (s, -1);
		for (int i = 1; i <= cnt; ++i)
		{
			int id = st[i].id;
			printf("%d ", id);
			if (st[i].flag)
			{
				printf("-\n");
			}
			else
			{
				printf("+\n");
			}
		}
	}
	return 0;
}

抱歉!评论已关闭.