MENU

SGU101 Domino(欧拉路径)

2018 年 06 月 07 日 • 阅读: 1239 • 图论阅读设置

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 -

链接

https://cn.vjudge.net/problem/SGU-101

题意

给定你n张骨牌,每张牌左右两端有一个数字,每张牌的左右两端数字可以颠倒,找出一种摆放骨牌的顺序,使得相邻骨牌的两端数字相同(最左边骨牌的最左端和最右边骨牌的最右端可以不管)。

题解

欧拉路径,把每个数字当做一个节点,每张牌当做一条边。

具体参考keambar题解cqbzwja题解,很详细。目前依然还有的一个问题是:保存路径的时候要先dfs下一条边因为当前点可能存在多条边,(1, 2)、(1, 3),如果先保存路径的话就会出现(1, 2, 3)这种错误,所以需要先dfs完,并且逆序输出。

代码

StatusAccepted
Time15ms
Memory1880kB
Length1795
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn = 1010;

struct Edge
{
    int id, next, to;
}edge[maxn];

int head[maxn], degree[maxn], ans[maxn];
bool vis[maxn];
int cnt = 0, tot = 0, s = 0;

void addedge(int u, int v, int id) // 链式前向星存储
{
    edge[cnt].to = v;
    edge[cnt].id = id;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

void dfs(int u) // dfs遍历
{
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int id = edge[i].id;
        int v = edge[i].to;
        if (!vis[abs(id)])
        {
            vis[abs(id)] = true;
            dfs(v);
            ans[++tot] = id;
        }
    }
}

bool check() // 判断欧拉路径两个基本条件
{
    int sum = 0;
    for (int i = 0; i <= 6; ++i)
        if (degree[i] & 1)
        {
            sum++;
            s = i;
        }
    if (sum == 1 || sum > 2) 
        return false;
    if (sum == 2)
        return true;
    for (int i = 0; i <= 6; ++i)
        if (degree[i] > 0)
        {
            s = i;
            break;
        }
    return true;
}

int main()
{
    memset(vis, 0, sizeof(vis));
    memset(head, -1, sizeof(head));
    int n, u, v;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d%d", &u, &v);
        addedge(u, v, i);
        addedge(v, u, -i);
        degree[u]++, degree[v]++;
    }
    if (!check())
        printf("No solution\n");
    else
    {
        dfs(s);
        for (int i = 1; i <= n; ++i)
            if (!vis[i])
            {
                printf("No solution\n");
                return 0;
            }
        for (int i = tot; i >= 1; --i)
        {
            if (ans[i] > 0)
                printf("%d +\n", ans[i]);
            else
                printf("%d -\n", -ans[i]);
        }
    }
    return 0;
}

复习了链式前向星,学习了欧拉回路与欧拉路径的判定。

The end.
2018-06-07 星期四
最后编辑于: 2018 年 07 月 26 日