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

---------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
 
using namespace std;
 
#define X(i) Edge[i].first
#define Y(i) Edge[i].second
typedef pair<int, int> pii;
 
const int maxn = 10;
const int maxm = 109;
const int n = 7;
 
struct edge {
int to, F, Id;
edge* next;
} E[maxm << 1], *pt = E, *head[maxn];
 
void AddEdge(int u, int v, int Id, int f) {
pt->to = v;
pt->F = f;
pt->Id = Id;
pt->next = head[u];
head[u] = pt++;
}
 
inline edge* Rev(edge* e) {
return E + ((e - E) ^ 1);
}
 
pii Edge[maxm];
int N, cnt[maxn], par[maxn];
stack<pii> S;
 
int Find(int x) {
return x == par[x] ? x : par[x] = Find(par[x]);
}
 
bool chk() {
int c = 0;
for(int i = 0; i < n; i++)
if((cnt[i] & 1) && ++c > 2) return false;
for(int i = 0; i < n; i++) par[i] = i;
for(int i = 0; i < N; i++)
par[Find(X(i))] = Find(Y(i));
c = -1;
for(int i = 0; i < n; i++) if(cnt[i]) {
if(!~c) 
c = par[i];
else if(par[i] != c)
return false;
}
return true;
}
 
void Euler(int x) {
for(edge*&e = head[x]; e; ) if(e->F) {
int F = e->F, t = e->to, Id = e->Id;
e->F = Rev(e)->F = 0;
e = e->next;
Euler(t);
S.push(make_pair(Id, F));
} else
e = e->next;
}
 
int main() {
scanf("%d", &N);
memset(cnt, 0, sizeof cnt);
for(int i = 0; i < N; i++) {
scanf("%d%d", &X(i), &Y(i));
cnt[X(i)]++;
cnt[Y(i)]++;
AddEdge(X(i), Y(i), i, 1);
AddEdge(Y(i), X(i), i, -1);
}
if(!chk()) {
puts("No solution");
return 0;
}
for(int i = 0; i < n; i++) if(cnt[i] & 1) {
Euler(i);
while(!S.empty()) {
pii p = S.top(); S.pop();
printf("%d %c\n", ++p.first, p.second != 1 ? '-' : '+');
}
return 0;
}
for(int i = 0; i < n; i++) if(cnt[i]) {
Euler(i);
while(!S.empty()) {
pii p = S.top(); S.pop();
printf("%d %c\n", ++p.first, p.second != 1 ? '-' : '+');
}
return 0;
}
return 0;
}

---------------------------------------------------------------------------

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 - 

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

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

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

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

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

  3. SGU 101 Domino【欧拉路径】

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

  4. Domino - SGU 101 (欧拉路径)

    题目大意:这是一个多米诺骨游戏,这个游戏的规则就是一个连着一个,现在给出 N 个多米诺,每个多米诺两边都有一个编号,相邻的多米诺的编号要一致,当然多米诺是可以翻转的(翻转就加‘-’,不翻转是‘+’), ...

  5. SGU 101.Domino (欧拉路)

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

  6. SGU 101 Domino 题解

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

  7. sgu 101 domino

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

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

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

  9. SGU 101

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

随机推荐

  1. sqlserver数据库 提纲

    sqlserver数据库一.体系结构文件--服务--界面 文件---.maf .ldf .ndf服务--MSSqlserver,别名(实例名)界面--宋庆龄serve如何windouws身份验证,如何 ...

  2. 一个证书多次使用-导出p12文件

    在苹果开发者网站申请的证书,是授权mac设备的开发或者发布的证书,这意味着一个设备对应一个证书,但是99美元账号只允许生成3个发布证书,两个开发证书,这满足不了多mac设备的使用,使用p12文件可以解 ...

  3. java/php/c#版rsa签名以及验签实现

    本文为转载,请转载请注明地址: 原文地址为        http://xw-z1985.iteye.com/blog/1837376 在开放平台领域,需要给isv提供sdk,签名是Sdk中需要提供的 ...

  4. tomcat 设置session 时间

    Tomcat  Session过期时间 Tomcat采用数据库连接池技术,当用户在一定时间不对数据库有操作时间后,就自动关闭这个连接,这是为了更好的利用资源,防止浪费宝贵的数据库连接资源. 可以采用如 ...

  5. python 文件夹比较

    参考:http://blog.csdn.net/imzoer/article/details/8675078 文件比较:filecmp模块:filecmp '''Created on 2014-6-6 ...

  6. Linux下为何都是文件的理解

    所谓“文件”,就是在我们的电脑中,以实现某种功能.或某个软件的部分功能为目的而定义的一个单位. Linux都是以文件的形式存在,当我们访问某个文件(Linux中的文件有目录,连接,普通文本),由于Li ...

  7. JDBC连接数据库概述

    直接介绍JDBC连接数据库的流程及其原理 创建一个以JDBC连接数据库的程序,包含7个步骤 1.加载JDBC数据库驱动 在连接数据库之前,首先要加载想要连接的数据库的驱动,就是数据库厂商提供的jar包 ...

  8. [原创]抢先DriverStudio夺取机器控制权(上篇)

    原文链接:抢先DriverStudio夺取机器控制权 废话不谈,言归正传!大家都知道,装了DriverStudio软件(我使用的是v3.2版)的系统在启动时会显示其配置画面,(如图0所示) 图 0 这 ...

  9. SQL Server MYSQL 检查点的好处

    执行检查点时: 如果一个事务不没有提交.日志缓存还是会被更新到磁盘上的物理文件. 这样做就使得大事务commit 时也特别快!

  10. android开源框架和开源项目(转)

    特效: http://www.androidviews.net/ http://www.theultimateandroidlibrary.com/ 常用效果: 1. https://github.c ...