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. .net 内存分配及垃圾回收总结

        生存期垃圾回收器 目前有很多种类型的垃圾回收器.微软实现了一种生存期垃圾回收器(Generation Garbage Collector). 生存期垃圾回收器将内存分为很多托管堆,每一个托管堆 ...

  2. hdu 4613 Points<计算几何>

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4613 题意: 判断一个集合中的点能不能由另一个集合中的点,通过平移,旋转,放缩得到~ 思路:先求出集合中的 ...

  3. RAP开发入门-开发笔记

    一.发布/运行 每次项目发布时需要在MANIFEST.MF->bulid中勾选依赖包.文件.代码等,避免报错 部署时项目可能会报一个baseline的错误,window->preferen ...

  4. ORACLE-RAC-11G-R2_INSTALL

    ORACLE 11.2.0.3 RAC INSTALL                                                                       20 ...

  5. Java动态替换InetAddress中DNS的做法简单分析2

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  6. iOS耳机操作

    iOS在7之后增加的麦克风权限的申请,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 AVAudioSession *avSession = [AVAudioSession shar ...

  7. Hadoop入门学习随笔

    推荐视频:慕课网http://www.imooc.com/video/8107 ===Hadoop是什么? 开源的.分布式存储+分布式计算平台. http://hadoop.apache.org == ...

  8. [转]ubuntu server上网配置

    [转]ubuntu server上网配置 http://blog.sina.com.cn/s/blog_6c9d65a101011pyt.html 今天我的ubuntu server上不去网了,所以重 ...

  9. perl DBI 学习总结(转载)

    perl DBI 学习总结 源文地址:http://blog.csdn.net/like_zhz/article/details/5441946 DBI和DBD的不同关系模型: ########### ...

  10. [vsftp服务]——ftp虚拟用户、权限设置等的实验

    搭建ftp服务器,满足以下要求: 1.允许匿名用户登录服务器并下载文件,下载速度设置为最高2MB/s 2.不允许本地用户登录ftp服务器 3.在服务器添加虚拟用户vuser01.vuser02.vus ...