SGU 101 Domino (输出欧拉路径)

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 -

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=101

 

 

把0-6当成点,输入的n个当成边。这样就形成了一个无向图。

答案就是求一个欧拉路径。

 

相关知识可以参考:http://blog.chinaunix.net/uid-26380419-id-3164913.html

 

一个是判断连通,然后度为奇数的点为0个或者2个,才有欧拉路径。

欧拉路径的求法dfs就可以了,很奇妙!

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2014-2-1 0:46:43
  4 File Name     :E:\2014ACM\SGU\SGU101.cpp
  5 ************************************************ */
  6 
  7 #include <stdio.h>
  8 #include <string.h>
  9 #include <iostream>
 10 #include <algorithm>
 11 #include <vector>
 12 #include <queue>
 13 #include <set>
 14 #include <map>
 15 #include <string>
 16 #include <math.h>
 17 #include <stdlib.h>
 18 #include <time.h>
 19 using namespace std;
 20 
 21 struct Edge
 22 {
 23     int to,next;
 24     int index;
 25     int dir;
 26     bool flag;
 27 }edge[220];
 28 int head[10],tot;
 29 void init()
 30 {
 31     memset(head,-1,sizeof(head));
 32     tot = 0;
 33 }
 34 void addedge(int u,int v,int index)
 35 {
 36     edge[tot].to = v;
 37     edge[tot].next = head[u];
 38     edge[tot].index = index;
 39     edge[tot].dir = 0;
 40     edge[tot].flag = false;
 41     head[u] = tot++;
 42     edge[tot].to = u;
 43     edge[tot].next = head[v];
 44     edge[tot].index = index;
 45     edge[tot].dir = 1;
 46     edge[tot].flag = false;
 47     head[v] = tot++;
 48 }
 49 int du[10];
 50 int F[10];
 51 int find(int x)
 52 {
 53     if(F[x] == -1)return x;
 54     else return F[x] = find(F[x]);
 55 }
 56 void bing(int u,int v)
 57 {
 58     int t1 = find(u);
 59     int t2 = find(v);
 60     if(t1 != t2)
 61         F[t1] = t2;
 62 }
 63 vector<int>ans;
 64 void dfs(int u)
 65 {
 66     for(int i = head[u]; i != -1;i = edge[i].next)
 67         if(!edge[i].flag )
 68         {
 69             edge[i].flag = true;
 70             edge[i^1].flag = true;
 71             dfs(edge[i].to);
 72             ans.push_back(i);
 73         }
 74 }
 75 
 76 int main()
 77 {
 78     //freopen("in.txt","r",stdin);
 79     //freopen("out.txt","w",stdout);
 80     int n;
 81     while(scanf("%d",&n) == 1)
 82     {
 83         init();
 84         int u,v;
 85         memset(du,0,sizeof(du));
 86         memset(F,-1,sizeof(F));
 87         for(int i = 1;i <= n;i++)
 88         {
 89             scanf("%d%d",&u,&v);
 90             addedge(u,v,i);
 91             du[u]++;
 92             du[v]++;
 93             bing(u,v);
 94         }
 95         int s = -1;
 96         int cnt = 0;
 97         for(int i = 0;i <= 6;i++)
 98         {
 99             if(du[i]&1) cnt++;
100             if(du[i] > 0 && s == -1)
101                 s = i;
102         }
103         bool ff = true;
104         if(cnt != 0 && cnt != 2)
105         {
106             printf("No solution\n");
107             continue;
108         }
109         for(int i = 0; i <= 6;i++)
110             if(du[i] > 0 && find(i) != find(s))
111                 ff = false;
112         if(!ff)
113         {
114             printf("No solution\n");
115             continue;
116         }
117         ans.clear();
118         if(cnt == 0)dfs(s);
119         else
120         {
121             for(int i = 0;i <= 6;i++)
122                 if(du[i] & 1)
123                 {
124                     dfs(i);
125                     break;
126                 }
127         }
128         for(int i = 0;i < ans.size();i++)
129         {
130             printf("%d ",edge[ans[i]].index);
131             if(edge[ans[i]].dir == 0)printf("-\n");
132             else printf("+\n");
133         }
134     }
135     return 0;
136 }

 

 

 

 

 

 

posted on 2014-02-03 11:18  kuangbin  阅读(1219)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: