SGU101欧拉回路

101. Domino

time limit per test: 0.5 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 -

 

将条件转换为一个图,0~6作为图中的顶点,每一张多米诺骨牌作为一条连接两点数的无向边,那么答案就是一条欧拉路径。

判定方法:一个图中存在欧拉路径,当且仅当这个图连通(此题中无需考虑),并且奇点数(即度数为奇数的顶点数)为0或者2。

若奇点数为0,则从任意一个顶点开始做一次dfs即可。

若奇点数为2,则从其中一个奇点开始做一次dfs即可。

注意:

1、不要忘了顶点0!

2、生成路径后要判断是否连通!

3、判断是否经过每一条边!

 

应该有多种dfs方式吧,这应该是个special judge吧,我看和样例不对,瞎改了半天,++;


 

/**********************
* author:crazy_石头
* Pro:SGU 101 Domino
* algorithm: Euler Road/use dfs to print path
* Judge Status:Accepted
* Memory:935K
* Time:31ms
* date:2013/11/02
***********************/

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep(i,h,n)  for(int i=(h);i<=(n);i++)
#define rrep(i,l,m) for(int i=(l);i>=(m);i--)

const int maxn=100+5;

int res[maxn][2],temp[maxn][2];

int tot,n;
int map[maxn][maxn],degree[maxn];
int vis[maxn];

inline void dfs(int s)
{
    rep(i,0,6)
    {
        if(map[s][i])
        {
            map[s][i]--;
            map[i][s]--;
            dfs(i);
            tot++;
            res[tot][0]=s;
            res[tot][1]=i;
        }
    }
}

inline void init()
{
    memset(map,0,sizeof(map));
    memset(degree,0,sizeof(degree));
    memset(vis,0,sizeof(vis));
}

int main()
{
    int s,deg,a,b;//deg用来记录某个顶点的度;
    scanf("%d",&n);
    init();
    rep(i,1,n)
    {
        scanf("%d%d",&a,&b);
        degree[a]++;
        degree[b]++;
        map[a][b]++;
        map[b][a]++;
        temp[i][0]=a;
        temp[i][1]=b;
    }

    rep(i,0,6)
    {
        if(degree[i])
        {
            s=i;//找到起点;
            break;
        }
    }
    deg=0;
    rep(i,0,6)
    {
        if(degree[i]&1)
        {
            deg++;//记录度数为奇数的点的个数;
            s=i;
        }
    }
    tot=0;
    if(deg!=0&&deg!=2)
    {
        puts("No solution");
        return 0;
    }
    dfs(s);
    if(tot<n)
    {
        puts("No solution");
        return 0;
    }
    else
    {
        rrep(i,tot,1)
        rep(j,1,n)
        {
            if(!vis[j]&&temp[j][0]==res[i][0]&&temp[j][1]==res[i][1])
            {
                printf("%d +\n",j);
                vis[j]=1;
                break;
            }
            else
            if(!vis[j]&&temp[j][0]==res[i][1]&&temp[j][1]==res[i][0])
            {
                printf("%d -\n",j);
                vis[j]=1;
                break;
            }
        }
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Borland )

你可能感兴趣的:(SGU101欧拉回路)