SGU 101 Domino 欧拉通路/回路

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 -
--------------------

首先判断能否形成回路

对于欧拉回路,直接求出即可。

对于欧拉通路,把起点与终点连接,输出时删去即可。

-----------------------

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#define clr(x,a) memset(x,a,sizeof(x))
using namespace std;
const int maxn=1111;
const int maxm=11111;
const int Special=0;
struct EdgeNode{
    int to;
    int idx;
    int next;
}edges[maxn];
int head[maxn],edge;
int n;
int deg[maxn];
bool vis[maxn];
bool hav[maxn];
void init(){
    clr(hav,0);
    clr(vis,0);
    clr(head,-1);
    clr(deg,0);
    edge=0;
}
void addedge(int u,int v,int idx){
    edges[edge].idx=idx,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
}

int m;
struct Domino{
    int l;
    int r;
}domi[maxn];

void dfs(int u){
    vis[u]=true;
    for (int i=head[u];i!=-1;i=edges[i].next){
        int v=edges[i].to;
        if (!vis[v]) dfs(v);
    }
}
int checkGraph(){
    for (int i=0;i<=6;i++) if (hav[i]){
        dfs(i);
        break;
    }
    for (int i=0;i<=6;i++) if (!vis[i]&&hav[i]) return -1;
    int odd=0,even=0;
    for (int i=0;i<=6;i++){
        if (deg[i]&1) odd++;
        else even++;
    }
    if (odd>2) return -1;
    if (odd==0) return 1;
    return 0;
}
bool mov[maxn];
stack<int>stk;
void euler(int u){
    //cerr<<"get u = "<<u<<endl;
    for (int i=head[u];i!=-1;i=edges[i].next){
        int v=edges[i].to;
        int id=edges[i].idx;
        if (mov[id]||mov[id^1]) continue;
        mov[id]=mov[id^1]=true;
        //cerr<<"try v = "<<v<<endl;
        euler(v);
        //cerr<<"push edge u = "<<u<<" v = "<<v<<endl;
        stk.push(id);
    }
}
void getEuler(int u){
    clr(mov,0);
    while (!stk.empty()) stk.pop();
    euler(u);
}
void solve(int cod){
    if (cod==-1){
        printf("No solution\n");
        return;
    }
    if (cod==1){
        for (int i=0;i<=6;i++) if (hav[i]){
            getEuler(i);
            break;
        }
        while (!stk.empty()){
            int id=stk.top();
            stk.pop();
            if (id&1) printf("%d -\n",id/2);
            else printf("%d +\n",id/2);
        }
        return;
    }
    if (cod==0){
        int u,v;
        u=v=-1;
        for (int i=0;i<=6;i++){
            if (deg[i]&1){
                if (u==-1) u=i;
                else v=i;
            }
        }
        //cerr<<"u v "<<u<<" "<<v<<endl;
        addedge(u,v,2*Special);
        addedge(v,u,2*Special+1);
        getEuler(u);
        int ans[maxn],cnt=0,p=0;
        while (!stk.empty()){
            int id=stk.top();
            stk.pop();
            if (id==2*Special||id==2*Special+1) p=cnt;
            ans[cnt++]=id;
        }
        //cerr<<"p="<<p<<endl;
        //system("pause");
        //for (int i=0;i<cnt;i++) cerr<<ans[i]<<" ";cerr<<endl;
        for (int i=p+1;i!=p;i>=cnt-1?i=0:i++){
            int id=ans[i];
            if (id&1) printf("%d -\n",id/2);
            else printf("%d +\n",id/2);
        }
    }
}

int main()
{
    scanf("%d",&n);
    init();
    for (int i=1;i<=n;i++) scanf("%d%d",&domi[i].l,&domi[i].r);
    for (int i=1;i<=n;i++){
        addedge(domi[i].l,domi[i].r,i*2);
        addedge(domi[i].r,domi[i].l,i*2+1);
        deg[domi[i].l]++;
        deg[domi[i].r]++;
        hav[domi[i].l]=hav[domi[i].r]=true;
    }
    int res=checkGraph();
    solve(res);
    return 0;
}




你可能感兴趣的:(SGU 101 Domino 欧拉通路/回路)