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. debian完整部署 Nginx + uWSGI + Django

    手工部署一个Django服务器真心不容易,需要安装很多东西.从头开始搭建服务器,主要是为了梳理一下后续开发中一般为碰到的平台部署.对后续问题的解决有一定帮助. 通常部署有2中方式: 一种是使用现成提供 ...

  2. Linux中printf格式化输出

    printf使用文本或者由空格分隔的参数,我们可以在printf中使用格式化字符串.printf不会写像echo那样自动添加换行符,必须手动添加 =========================== ...

  3. Python数据类型-----数字&字符串

    Python数字类型 int类型表示的范围:-2147483648至2147483648之间,超出这个范围的数字即视为long(长整形) 在Python中不需要事先声明数据类型,它是根据具体的赋值来进 ...

  4. Php+Redis 实现Redis提供的lua脚本功能

    <?php require_once "predis-0.8/autoload.php"; $config['schema'] = 'tcp'; $config['host' ...

  5. golang:interface{}类型测试

    在golang中空的interface即interface{}可以看作任意类型, 即C中的void *. 对interface{}进行类型测试有2种语法: 1. Comma-ok断言: value, ...

  6. Go中简单的文件读写

    Go中的ioutil包可以方便的实现文件读写.代码: package main import ( "fmt" "io/ioutil" ) func main() ...

  7. 修改linux端口范围 ip_local_port_range

    tags: ip_local_port_range 端口范围 sysctl Linux中有限定端口的使用范围,如果我要为我的程序预留某些端口,那么我需要控制这个端口范围, 本文主要描述如何去修改端口范 ...

  8. 利用LibreOffice转换ppt、doc转化pdf

    利用LibreOffice转换ppt.doc转化pdf LibreOffice下载地址:  http://www.libreoffice.org/download/libreoffice-fresh/ ...

  9. 电梯调度--c++--软件工程

    一.设计思路 (1)将乘客要去的楼层数存起来. (2)假设yi为乘客要爬楼层数之和,yi=n1*|(n1-ni)|+n2*|(n2-ni)|+..+n18*|(n18-ni)| (3)比较y1到y18 ...

  10. 校园导游之NABC个人分析

    校园导游之NABC个人分析 Need: 为不熟悉校园环境的人们(如新生,来咱们学校参观滴)提供便利. Approach: 了解Andriod应用开发:导航功能之外还可以对学校进行宣传,比如拍一些学校的 ...