SGU101 Domino
Eulerian path. Note that the graph may be disconnected.
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 -
// SGU 101 -- Domino
import java.util.Scanner;
public class Solution {
private static final int Maxm = 100 + 10;
private static final int Maxn = 7;
private static int n;
private static int l = 1;
private static int sum = 0;
private int pre[], last[], other[], w[];
private int id[], zt[];
private boolean v[];
Solution() {
pre = new int [Maxm << 1];
last = new int [Maxn];
other = new int [Maxm << 1];
w = new int [Maxm << 1];
id = new int [Maxn];
zt = new int [Maxm << 1];
v = new boolean [Maxm << 1];
}
private void Add_edge(int p, int q, int num) {
pre[++l] = last[p]; last[p] = l; other[l] = q; w[l] = num;
pre[++l] = last[q]; last[q] = l; other[l] = p; w[l] = -num;
}
private void Init() {
Scanner conin = new Scanner(System.in);
n = conin.nextInt();
for (int i = 1; i <= n; ++i) {
int x = conin.nextInt();
int y = conin.nextInt();
Add_edge(x, y, i);
++id[x]; ++id[y];
}
conin.close();
}
private void Dfs(int p) {
for (int q = last[p]; q > 0; q = pre[q]) {
if (!v[q]) {
v[q] = true;
v[q ^ 1] = true;
Dfs(other[q]);
zt[++sum] = w[q];
}
}
}
private void Work() {
int cnt = 0, first = -1;
for (int i = 0; i < Maxn; ++i)
if ((id[i] & 1) == 1) {
++cnt;
first = i;
}
if (cnt != 0 && cnt != 2) {
System.out.println("No solution");
return;
}
if (first != -1)
Dfs(first);
else
for (int i = 0; i < Maxn; ++ i)
if (id[i] != 0) {
Dfs(i);
break;
}
if (sum != n) {
System.out.println("No solution");
return;
}
for (int i = sum; i >= 1; --i) {
if (zt[i] > 0)
System.out.printf("%d +\n", zt[i]);
else
System.out.printf("%d -\n", -zt[i]);
}
}
public static void main(String[] args) {
Solution work = new Solution();
work.Init();
work.Work();
}
}A quick summary of Eulerian paths
-
An undirected graph has an Eulerian circuit iff every vertex has even degree
-
An undirected graph has an Eulerian trail iff zero or two vertices have odd degree
-
A directed graph has an Eulerian circuit iff every vertex has equal in-degree and out-degree
-
A directed graph has an Eulerian trail iff at most one vertex has in-degree = out-degree + 1, at most one vertex has in-degree = out-degree − 1, and every other vertex has equal in-degree and out-degree (or all vertices do).