SGU 101.Domino (Euler path)

Source: Internet
Author: User

Find Euler path ... Direct DFS, Time complexity O (N)

---------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;#define X (i) Edge[i].first#define Y (i) Edge[i].secondtypedef pair<int, int> PII;const int MAXN = ten;const int MAXM = 109;const int n = 7;struct Edge {int to, F, Id;edge* Next;} E[MAXM << 1], *pt = E, *HEAD[MAXN]; void Addedge (int u, int v, int Id, int f) {pt->to = v;pt->f = F;pt->id = Id;pt->next = Head[u];Head[u] = pt++;} inline edge* Rev (edge* e) {return E + ((e-e) ^ 1);}PII EDGE[MAXM];int N, CNT[MAXN], PAR[MAXN];stack<pii> S;int Find (int x) {return x = = Par[x]? x:par[x] = Find (par[x]);}bool Chk () {int c = 0;for (int i = 0; i < n; i++)if ((Cnt[i] & 1) && ++c > 2) return false;for (int i = 0; i < n; i++) par[i] = i;for (int i = 0; i < N; i++)Par[find (X (i))] = Find (Y (i));C =-1;for (int i = 0; i < n; i++) if (Cnt[i]) {if (!~c)C = par[i];else if (par[i]! = c)return false;}return true;}void Euler (int x) {For (edge*&e = head[x]; e;) if (e->f) {int F = e->f, t = e->to, Id = e->id;e->f = Rev (e)->f = 0;e = e->next;Euler (t);S.push (Make_pair (Id, F));} elsee = e->next;}int main () {scanf ("%d", &n);memset (CNT, 0, sizeof CNT);for (int i = 0; i < N; i++) {scanf ("%d%d", &x (i), &y (i));cnt[x (i)]++;cnt[y (i)]++;Addedge (X (i), Y (i), I, 1);Addedge (Y (i), X (i), I,-1);}if (!chk ()) {puts ("No solution");return 0;}for (int i = 0; i < n; i++) if (Cnt[i] & 1) {Euler (i);While (! S.empty ()) {PII p = s.top (); S.pop ();printf ("%d%c\n", ++p.first, P.second! = 1? '-': ' + ');}return 0;}for (int i = 0; i < n; i++) if (Cnt[i]) {Euler (i);While (! S.empty ()) {PII p = s.top (); S.pop ();printf ("%d%c\n", ++p.first, P.second! = 1? '-': ' + ');}return 0;}return 0;}

---------------------------------------------------------------------------

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 P IPs, on it face. The blocks usually is called bones, dominoes, or pieces and sometimes men, stones, or even cards.
The face of each piece are divided, by a line or ridge, into and squares, each of the which are marked as would be a pair of dic E...

The principle in nearly all modern dominoes games are to match one end of a piece to another so is identically or recipro Cally numbered.

Encyclopædia BRITANNICA

Given a set of domino pieces where each side are marked with the digits from 0 to 6. Your task is to arrange pieces in a line such the, that they touch through equal marked sides. It's 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 P Ieces in the domino set. The following N lines describe pieces. Each piece was represented on a separate line in a form of the digits from 0 to 6 separated by a space.

Output

Write "No Solution" if it is the impossible to arrange them described. If It is a possible, write any of the. Pieces must is written in left-to-right order. Every of N lines must contains number of current Domino piece and sign "+" or "-" (first means so you don't rotate that pi ECE, and second if you rotate it).

Sample Input

Sample Output

SGU 101.Domino (Euler path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.