sgu 101. Domino 歐拉回路

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 -


歐拉路徑

#include <bits/stdc++.h>

using namespace std;

int v;
int tot=0;
int head[10];
int res[105];
int du[10];

struct node{
    int to;
    int next;
    int id;
    int v;
};

node edge[205];

void add_edge(int a,int b,int c){
    edge[tot].v=1;
    edge[tot].id=c;
    edge[tot].to=b;
    edge[tot].next=head[a];
    head[a]=tot++;

    edge[tot].v=1;
    edge[tot].id=c;
    edge[tot].to=a;
    edge[tot].next=head[b];
    head[b]=tot++;
}

void dfs(int x){
    for(int i=head[x];i!=-1;i=edge[i].next)if(edge[i].v){
        edge[i].v=0;
        edge[i^1].v=0;
        dfs(edge[i].to);
        if(i%2==0){
            res[v--]=edge[i].id;
        }
        else{
            res[v--]=-edge[i].id;
        }
    }

}

void solve(){
    int n;
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        du[a]++;
        du[b]++;
        add_edge(a,b,i);
    }
//    cout<<endl;
//    for(int i=0;i<=6;i++){
//        cout<<du[i]<<" ";
//    }
//    cout<<endl;
    int odd=0;
    for(int i=0;i<=6;i++){
        if(du[i]%2==1){
            odd++;
        }
    }

    if(odd!=0&&odd!=2){
        printf("No solution");
        return;
    }

    int be;
    for(int i=0;i<=6;i++){
        if(du[i]!=0){
            be=i;
        }
        if(du[i]%2==1){
            break;
        }
    }
    v=n;
    dfs(be);
    if(v!=0){
        printf("No solution");
        return;
    }
    for(int i=1;i<=n;i++){
        printf("%d %s\n",abs(res[i]),res[i]>0?"+":"-");
    }
}

int main(){
    solve();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章