欧拉道路

判断的条件不必多说了。

关键点在于在dfs输出欧拉道路的时候要倒着输出。


sgu 101

Description

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<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const int MAXN=110;

int a[MAXN],b[MAXN],fa[MAXN];
int num[10],ex[10];
int n;
int l=0;
int findset(int x)
{
    if(x==fa[x]) return x;
    else return (fa[x]=findset(fa[x]));
}
int vis[MAXN],ansOut[MAXN],flagOut[MAXN];
void dfs(int x)
{
    for(int i=1;i<=n;i++)
        if(!vis[i])
        {
            if(a[i]==x)
            {
                vis[i]=1;
                dfs(b[i]);
                ansOut[l]=i;
                flagOut[l]=0;
                l++;
            }
            else if(b[i]==x)
            {
                vis[i]=1;
                dfs(a[i]);
                ansOut[l]=i;
                flagOut[l]=1;
                l++;
            }
        }
}
int main(void)
{
#ifdef LOCAL
    freopen("in.txt","rb",stdin);
    //freopen("out.txt","wb",stdout);
#endif
    scanf("%d",&n);
    memset(num,0,sizeof(num));
    memset(ex,0,sizeof(ex));
    for(int i=0;i<=6;i++) fa[i]=i;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        int aa=a[i],bb=b[i];
        num[aa]++;
        num[bb]++;
        if(findset(aa)!=findset(bb)) fa[findset(aa)]=findset(bb);
        ex[aa]=ex[bb]=1;
    }
    int sum=0;
    for(int i=0;i<=6;i++) if(ex[i] && fa[i]==i) sum++;
    if(sum!=1) {printf("No solution\n"); exit(0);}
    int tot=0;
    for(int i=0;i<=6;i++) if(num[i]%2) tot++;
    if(tot!=0 && tot!=2) {printf("No solution\n"); exit(0);}
    if(tot==0) dfs(a[1]);
    else
    {
        int st=0;
        for(int i=0;i<=6;i++) if(num[i]%2) {st=i;break;}
        l=0;
        memset(vis,0,sizeof(vis));
        dfs(st);
    }
    for(int i=0;i<l;i++)
    {
        if(flagOut[i]) printf("%d +\n",ansOut[i]);
        else printf("%d -\n",ansOut[i]);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值