SGU101
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的更多相关文章
- SGU---101 无向图的欧拉回路
题目链接: https://cn.vjudge.net/problem/SGU-101 题目大意: 给定你n张骨牌,每张牌左右两端有一个数字,每张牌的左右两端数字可以颠倒,找出一种摆放骨牌的顺序,使得 ...
- sgu101 欧拉路径 难度:1
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...
- SGU101 求有重边的无向图欧拉迹
题意:好多木棒,俩端有数字(0--6)标记,按数字相同的端首尾相连成一条直线(木棒可以相同).即求有重边的无向图欧拉迹. 先判定是否为欧拉图,俩个条件,不说了.如果是欧拉图,输出路经. 方法:dfs遍 ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
- SGU 101 Domino (输出欧拉路径)
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...
- SGU 乱搞日志
SGU 100 A+B :太神不会 SGU 101 Domino: 题目大意:有N张骨牌,两张骨牌有两面有0到6的数字,能相连当且仅当前后数字相同,问能否有将N张骨牌连接的方案?思路:裸的欧拉回路,注 ...
随机推荐
- jquery easyui datebox单击文本框显示日期选择
jquery easyui的datebox日历控件,实现单击文本框出现日历选择,如下图: 代码: 修改jquery.easyui.min.js第9797行函数(jQuery EasyUI 1.3.2) ...
- hdu 5280 Senior's Array
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5280 Senior's Array Description One day, Xuejiejie ge ...
- 快速的搭建JFinal的ORM框架示例
JFinal默认用的是Freemarker作为视图. 所以,打架还是准备好俩个jar包吧! freemarker-2.3.16.jar JFinal-bin-1.5.jar 新建web工程和添加lib ...
- iOS进阶学习-CoreData
一.CoreData数据库框架的优势 1.CoreData数据持久化框架是Cocoa API的一部分,首次在iOS5版本的系统中出现,它允许按照实体-属性-值模型组织数据,并以XML.二进制文件或者S ...
- 关于开始学习Leetcode的第一帖
从明天开始,白天在实验室完成工作,晚上来图书馆换个环境去学习算法数据结构等计算机基础性的技能.在LeetCode这个平台上做题. 现在感觉自己在算法和数据机构这方面实在是太薄弱了,需要慢慢的捡起来来, ...
- ExtJS4.x 开发环境搭建
需要的资源 ExtJS4.2 eclipse 开发环境搭建 在项目中国需要引用的文件: eclipse中有报错.需要处理的是ext-lang-zh_CN.js,中文编码不能识别.右键->属性-& ...
- [转]windows 软链接的建立及删除
[转]windows 软链接的建立及删除 http://blog.chinaunix.net/uid-74941-id-3764093.html 1.建立举例 ##建立d:develop链接目录,指向 ...
- Oracle小技巧
Oracle纵向变横向,多行变一行 取处方下的药品大类,以 处方 药品大类 a001 中药.西药,中成药的 的格式显示 /*处方与药品大类的对应 周璇球 20130829*/ SELEC ...
- UISlider swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- UIScrowView swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...