SGU 101

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 -
题意是给你n个多米诺骨牌,正反面各有一个数字,你只能把相同的数字的一面对在一起,问你如何排列他们。

这题居然是欧拉路!!!没想到啊!!!

不过仔细一想就是欧拉路,最多有两个节点入度为奇。

/*
ID: XMzhou
LANG: C++
TASK: Domino
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair

const int MAXN = 20000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-10
const long long MOD = 1000000000 + 7;
const int mod = 10007;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair pii;
typedef vector vec;
typedef vector mat;


#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
struct Edge
{
    int from, to;
};
vector  G[MAXN];
vector  edges;
int a[MAXN];///统计每个节点的度数
vector  E;
int vis[MAXN];
void dfs(int u)
{
    for(int i = 0 ; i < G[u].size() ; i++)
    {
        int m = G[u][i];
        Edge &e = edges[G[u][i]];
        int v = e.to;
        if(vis[m])continue;
        vis[m] = vis[m ^ 1] = 1;///m为奇数时m^1 = m - 1,m为偶数时m^1=m + 1
        dfs(v);
        E.push_back(m);
    }
}
int main()
{
    //ios::sync_with_stdio(false);
    #ifdef Online_Judge
        freopen("clocks.in","r",stdin);
        freopen("clocks.out","w",stdout);
    #endif // Online_Judge
    int n;
    while(~scanf("%d" , &n))
    {
        for(int i = 0 ; i < 7 ; i++)G[i].clear();
        edges.clear();
        E.clear();
        clr(a , 0);
        int ans = 0;
        for(int i =0  ;i < n; i ++)
        {
            int x , y;
            scanf("%d%d" , &x , &y);
            a[x]++,a[y]++;
            edges.push_back((Edge){x , y});
            int m = edges.size();
            G[x].push_back(m - 1);
            edges.push_back((Edge){y , x});
            m = edges.size();
            G[y].push_back(m - 1);
        }
        for(int i = 0  ; i< 7 ; i++)if(a[i])
        {
            ans = i;
            break;
        }
        clr(vis , 0);
        int cnt = 0;
        for(int i =0 ; i < 7 ; i++)
            if(a[i] % 2)
            {
                ans = i;
                cnt++;
            }
        if(!(cnt == 0 || cnt == 2))
        {
            puts("No solution");
            continue;
        }
        dfs(ans);
        if(E.size() != n)
        {
            puts("No solution");
            continue;
        }
        for(int i = E.size() - 1 ; i >= 0 ; i--)
        {
            printf("%d " , E[i] / 2 + 1);
            if(E[i] % 2)printf("-\n");
            else printf("+\n");
        }
    }
    return 0;
}


你可能感兴趣的:(SGU,连通性,最短路)