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 - 找一条欧拉通路
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector> using namespace std; #define maxn 105 int n,sp,len = ;
int fa[],deg[],first[],v1[ * maxn],next[ * maxn],ans1[maxn];
bool done[],vis[ * maxn];
bool flag = ;
char ans2[maxn]; int _find(int x) {
return x == fa[x] ? x : fa[x] = _find(fa[x]);
} void _union(int x,int y) {
int u = _find(x),v = _find(y); if(u != v) fa[u] = v;
}
void judge() {
int pos = ;
while(pos <= && !done[pos]) pos++; sp = pos; int now = fa[pos];
int odd = ;
for(int i = ; i <= ; i++) {
if(!done[i]) continue;
if(deg[i] % ) {
odd++;
sp = i;
}
int u = _find(i);
if(u != now) flag = ;
} if(odd > ) flag = ; } void output(int u) {
//printf("u = %d\n",u);
for(int e = first[u]; e != -; e = next[e]) { if(!vis[e]) {
vis[e] = ;
int t = (e % ) ? - : ;
vis[e + t] = ;
output(v1[e]); char c = t == - ? '-' : '+';
//printf("u = %d v = %d\n",u,v1[e]);
ans1[len] =e / + ;
ans2[len++] = c; }
} } void addedge(int a,int b,int id) {
int e = first[a];
next[id] = e;
first[a] = id;
v1[id] = b; }
void solve() { judge(); if(flag) {
output(sp);
for(int i = len - ; i >= ; i--)
printf("%d %c\n",ans1[i],ans2[i]);
}
else printf("No solution\n"); }
int main()
{ // freopen("sw.in","r",stdin);
scanf("%d",&n); //printf("n = %d\n",n); for(int i = ; i <= ; i++) {
fa[i] = i;
first[i] = -;
} for(int i = ; i < * n; i += ) {
int a,b;
scanf("%d%d",&a,&b);
done[a] = ;
done[b] = ;
_union(a,b);
deg[a]++;
deg[b]++;
addedge(a,b,i);
addedge(b,a,i + );
} solve(); //cout << "Hello world!" << endl;
return ;
}

SGU101的更多相关文章

  1. SGU---101 无向图的欧拉回路

    题目链接: https://cn.vjudge.net/problem/SGU-101 题目大意: 给定你n张骨牌,每张牌左右两端有一个数字,每张牌的左右两端数字可以颠倒,找出一种摆放骨牌的顺序,使得 ...

  2. sgu101 欧拉路径 难度:1

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...

  3. SGU101 求有重边的无向图欧拉迹

    题意:好多木棒,俩端有数字(0--6)标记,按数字相同的端首尾相连成一条直线(木棒可以相同).即求有重边的无向图欧拉迹. 先判定是否为欧拉图,俩个条件,不说了.如果是欧拉图,输出路经. 方法:dfs遍 ...

  4. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

  5. SGU 101 Domino (输出欧拉路径)

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...

  6. SGU 乱搞日志

    SGU 100 A+B :太神不会 SGU 101 Domino: 题目大意:有N张骨牌,两张骨牌有两面有0到6的数字,能相连当且仅当前后数字相同,问能否有将N张骨牌连接的方案?思路:裸的欧拉回路,注 ...

随机推荐

  1. 【原创】StickHeaderListView的简单实现,解决footerView问题

    1.前言: 前几天用了GitHub上se.emilsjolander.stickylistheaders这个组件,然后发现这个组件的listview不能添加footerView,加了footer后,滑 ...

  2. C基础 那些年用过的奇巧淫技

    引言 - 为寻一颗明星 为要寻一颗明星 徐志摩 1924年12月1日<晨报六周年纪念增刊> 我骑著一匹拐腿的瞎马, 向著黑夜里加鞭:—— 向著黑夜里加鞭, 我跨著一匹拐腿的瞎马.// 我冲 ...

  3. EMVTag系列16《AC响应数据》

    在一个联机交易中,要传送到发卡行的专有应用数据. 字段 长度(字节) 赋值 说明 长度 1 07 分散密钥索引 1 00 密文版本号 1 01 根据发卡行密钥版本设置 卡片验证结果(CVR) 4 03 ...

  4. 【转】Winform下KeyDown,KeyPress,KeyUp事件的总结

    http://blog.csdn.net/xiashengwang/article/details/6777907

  5. Maven系列--pom.xml 配置详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. Hi java新特性

    java新特性 1995.5.23 java语言 1996 jdk1.0 250个类在API 主要用在桌面型应用程序1997 jdk1.1 500 图形用户界面编程1998 jdk1.2 2300 J ...

  7. Power Map 入门

    Excel 的 Microsoft Power Map是三维 (3-D) 数据的可视化工具,允许您以新的方式看信息.电源映射允许您发现您可能看不到传统的二维 (2-d) 表和图中的见解. 使用Powe ...

  8. Go support for Android

    Go support for Android David Crawshaw June 2014 Abstract We propose to introduce Go support for the ...

  9. JVM学习总结一——内存模型

    JVM是java知识体系的基石之一,任何一个java程序的运行,都要借助于他.或许对于我这种初级程序员而言,工作中很少有必要刻意去关注JVM,然而如果能对这块知识有所了解,就能够更清晰的明白程序的运行 ...

  10. SQLite之写一个表

    1.首先你需要一个路径. 获取document目录并返回数据库目录 - (NSString *)dataFilePath{ NSArray *paths = NSSearchPathForDirect ...