# Domino

## 题目描述

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.

## 代码

#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct IOManager {
template <typename T>
char c; bool flag = false; x = 0;
while (~ c && ! isdigit(c = getchar()) && c != '-') ;
c == '-' && (flag = true, c = getchar());
if (! ~ c) return false;
while (isdigit(c)) x = (x << 3) + (x << 1) + c - '0', c = getchar();
return (flag && (x = -x), true);
}
c = '\n';
while (~ c && ! (isprint(c = getchar()) && c != ' ')) ;
return ~ c;
}
char c; int len = 0;
while (~ c && ! (isprint(c = getchar()) && c != ' ')) ;
if (! ~ c) return 0;
while (isprint(c) && c != ' ') s[len ++] = c, c = getchar();
return (s[len] = '\0', len);
}
template <typename T>
inline IOManager operator > (T &x) {
}
template <typename T>
inline void write(T x) {
x < 0 && (putchar('-'), x = -x);
x > 9 && (write(x / 10), true);
putchar(x % 10 + '0');
}
inline void write(char c) {
putchar(c);
}
inline void write(char s[]) {
int pos = 0;
while (s[pos] != '\0') putchar(s[pos ++]);
}
template <typename T>
inline IOManager operator < (T x) {
write(x); return *this;
}
} io;

struct Solver {
private:
static const int N = 110;
static const int C = 10;
int n, cnt, mp[C][C], in[C];
bool vis[N];
pair <int, int> line[N];
vector <pair <int, int> > ans;
void input() {
io > n;
for (int i = 1; i <= n; ++ i) {
io > line[i].first > line[i].second;
++ mp[line[i].first][line[i].second], ++ mp[line[i].second][line[i].first];
++ in[line[i].first], ++ in[line[i].second];
}
}
void dfs(int t) {
for (int i = 0; i < C; ++ i)
if (mp[t][i]) -- mp[t][i], --mp[i][t], dfs(i), ++ cnt, ans.push_back(make_pair(t, i));
}
void process() {
cnt = 0; int s = -1;
for (int i = 0; i < C; ++ i)
if (in[i] & 1) ++ cnt, s = i;
else if (in[i] && ! ~ s) s = i;
if (cnt > 2) io < (char *) "No solution\n";
else {
cnt = 0, dfs(s);
if (cnt < n) io < (char *) "No solution\n";
else {
for (int i = ans.size() - 1; ~ i; -- i)
for (int j = 1; j <= n; ++ j)
if (! vis[j]) {
if (line[j].first == ans[i].first && line[j].second == ans[i].second) {
io < j < ' ' < '+' < '\n', vis[j] = true; break;
}
if (line[j].first == ans[i].second && line[j].second == ans[i].first) {
io < j < ' ' < '-' < '\n', vis[j] = true; break;
}
}
}
}
}

public:
void solve() { input(), process(); }
} solver;

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


418 I'm a teapot