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就可以了,很奇妙!

 /* ***********************************************
Author :kuangbin
Created Time :2014-2-1 0:46:43
File Name :E:\2014ACM\SGU\SGU101.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; struct Edge
{
int to,next;
int index;
int dir;
bool flag;
}edge[];
int head[],tot;
void init()
{
memset(head,-,sizeof(head));
tot = ;
}
void addedge(int u,int v,int index)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].index = index;
edge[tot].dir = ;
edge[tot].flag = false;
head[u] = tot++;
edge[tot].to = u;
edge[tot].next = head[v];
edge[tot].index = index;
edge[tot].dir = ;
edge[tot].flag = false;
head[v] = tot++;
}
int du[];
int F[];
int find(int x)
{
if(F[x] == -)return x;
else return F[x] = find(F[x]);
}
void bing(int u,int v)
{
int t1 = find(u);
int t2 = find(v);
if(t1 != t2)
F[t1] = t2;
}
vector<int>ans;
void dfs(int u)
{
for(int i = head[u]; i != -;i = edge[i].next)
if(!edge[i].flag )
{
edge[i].flag = true;
edge[i^].flag = true;
dfs(edge[i].to);
ans.push_back(i);
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
while(scanf("%d",&n) == )
{
init();
int u,v;
memset(du,,sizeof(du));
memset(F,-,sizeof(F));
for(int i = ;i <= n;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v,i);
du[u]++;
du[v]++;
bing(u,v);
}
int s = -;
int cnt = ;
for(int i = ;i <= ;i++)
{
if(du[i]&) cnt++;
if(du[i] > && s == -)
s = i;
}
bool ff = true;
if(cnt != && cnt != )
{
printf("No solution\n");
continue;
}
for(int i = ; i <= ;i++)
if(du[i] > && find(i) != find(s))
ff = false;
if(!ff)
{
printf("No solution\n");
continue;
}
ans.clear();
if(cnt == )dfs(s);
else
{
for(int i = ;i <= ;i++)
if(du[i] & )
{
dfs(i);
break;
}
}
for(int i = ;i < ans.size();i++)
{
printf("%d ",edge[ans[i]].index);
if(edge[ans[i]].dir == )printf("-\n");
else printf("+\n");
}
}
return ;
}

SGU 101 Domino (输出欧拉路径)的更多相关文章

  1. SGU 101 Domino【欧拉路径】

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=101 题意: N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转 ...

  2. SGU 101.Domino( 欧拉路径 )

    求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...

  3. sgu 101 Domino 解题报告及测试数据

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求多米诺骨牌按照一定方式放置能否使相邻的位置 ...

  4. SGU 101.Domino (欧拉路)

    时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个   方形,两边 ...

  5. SGU 101 Domino 题解

    鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个 ...

  6. sgu 101 domino

    题意还算简洁明了,加上有道翻译凑过着读完了题.题意大体上是 给你 n 个多米诺骨牌, 给出每个骨牌两端的数字, 只有数字相同才可以推到, 比如 2-3和3-2.你可以旋转这些多米诺骨牌, 输出一个可以 ...

  7. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  8. poj 2337 有向图输出欧拉路径

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 Descrip ...

  9. SGU 101

    SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...

随机推荐

  1. linq中let关键字学习

    linq中let关键字就是对子查询的一个别名,let子句用于在查询中添加一个新的局部变量,使其在后面的查询中可见. linq中let关键字实例 1.传统下的子查询与LET关键字的区别     C# 代 ...

  2. 20155230 2016-2017-2 《Java程序设计》第九周学习总结

    20155230 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 statement在不使用时所关联的resultset也会自动关闭. 要让SQL执 ...

  3. HttpClient与HttpUrlConnection下载速度比较

    Android有两套http的API,刚开始使用网络编程时多少有些迷惑到底用哪个好呢?其实孰优孰劣无需再争论,google已经指出HttpUrlConnection是Android更优的选择,并在SD ...

  4. 第7月第20天 epoll

    1. ) { struct sockaddr in_addr; socklen_t in_len; int infd; char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; ...

  5. Python 入门基础1 --语言介绍

    本节目录: 一.编程语言介绍 二.python解释器介绍 三.安装python解释器 四.运行python程序的两种方式 五.变量 六.后期补充内容 一.编程语言介绍 1.机器语言: 直接用二进制编程 ...

  6. mybatis多对多关联查询——(十)

    1.需求 查询用户及用户购买商品信息. 2     sql语句 查询主表是:用户表 关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所以关联表: orders.orderdetail. ...

  7. word技巧

    1.插入注解(脚注和尾注) 2.复制的图片显示不全怎么办? 横向显示,或者图片另存为然后保存为PPT 3.word修订标记的添加和删除(最终版) 4.word中表格样式调整 5.修改标题的样式和标题的 ...

  8. Python-Analysis-Malware

    Python恶意软件分析应用-PEfile 0x1.前言 要想对恶意代码快速分析,Python是一门必须要掌握的编程技能.因为它是跨平台的,而且容易阅读和编写.许多开源安全工具也是用Python写的. ...

  9. linux内核环形缓冲区【转】

    转自:https://blog.csdn.net/eydwyz/article/details/56671023 循环缓冲区在一些竞争问题上提供了一种免锁的机制,免锁的前提是,生产者和消费 都只有一个 ...

  10. 浅谈tomcat中间件的优化【转】

    今天来总结一下tomcat的一些优化的方案,由于本人才疏学浅,写的不好,勿喷! tomcat对于大多数从事开发工作的童鞋应该不会很陌生,通常做为默认的开发环境来为大家服务,不过tomcat默认的一些配 ...