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. CSS-3 box-shadow 的使用

    box-shadow是给对象实现图层阴影效果的. 语法: E {box-shadow: <length> <length> <length>?<length& ...

  2. pytorch之LSTM

    from:http://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/#recurrent-layers class ...

  3. Three.js基础探寻三——透视投影照相机

    本篇主要介绍Three.js照相机中的透视投影照相机. 上一篇:正交投影照相机 5.透视投影照相机构造函数 透视投影照相机(Perspective Camera)的构造函数是: THREE.Persp ...

  4. Linux Power Managment详解 【转】

    转自:http://blog.chinaunix.net/uid-24517893-id-254740.html Linux Power Managment 谨以此文纪念过往的岁月 一.前言 在这个对 ...

  5. python自动安装mysql5.7【转】

    #!/usr/bin/env python import os import sys import re base_dir = '/opt/software/mysql-5.7.17-linux-gl ...

  6. 002_tmux详解

    参考下赖老师的: http://mingxinglai.com/cn/2012/09/tmux/ 一. 二. http://wdxtub.com/2016/03/30/tmux-guide/   (待 ...

  7. mysql 字符编码设置

    安装mysql时如果字符编码为默认值latin1,则需要修改为utf8以便支持中文数据. 命令如下: 1.显示数据库字符集 mysql> show create database test;+- ...

  8. kafka在zookeeper中存储结构

    1.topic注册信息 /brokers/topics/[topic] : 存储某个topic的partitions所有分配信息 Schema:   {    "version": ...

  9. Laravel中使用自己的类库三种方式

    虽然Composer使得我们可以重用很多现有的类库(例如packagist.org中的),但是我们仍然可能用到一些不兼容composer的包或者类库.另外在某一项目中,我们也可能会创建某一类库,而且可 ...

  10. SqlServer Case when then用法总结

    SELECT d.DicName , --DevelopMode ISNULL(NULL,NULL) , --Orgid b.FullName , --Areid c.DicName , --Inve ...