SGU 101 Domino (输出欧拉路径)
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 (输出欧拉路径)的更多相关文章
- SGU 101 Domino【欧拉路径】
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=101 题意: N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转 ...
- SGU 101.Domino( 欧拉路径 )
求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...
- sgu 101 Domino 解题报告及测试数据
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求多米诺骨牌按照一定方式放置能否使相邻的位置 ...
- SGU 101.Domino (欧拉路)
时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
- SGU 101 Domino 题解
鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个 ...
- sgu 101 domino
题意还算简洁明了,加上有道翻译凑过着读完了题.题意大体上是 给你 n 个多米诺骨牌, 给出每个骨牌两端的数字, 只有数字相同才可以推到, 比如 2-3和3-2.你可以旋转这些多米诺骨牌, 输出一个可以 ...
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- poj 2337 有向图输出欧拉路径
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10186 Accepted: 2650 Descrip ...
- SGU 101
SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...
随机推荐
- angularJs入门篇-hello world 开头
AngularJS 采用了完全不同的解决方案,它创建实时视图模板代替视图,而不是将数据合并进模板之后更新DOM. 任何一个独立视图组件中的值都是 动态替换的.这个功能可以说是AngularJS中最重要 ...
- 20155212 2016-2017-2 《Java程序设计》第8周学习总结
20155212 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 Chapter14 1. Channel架构与操作 想要取得Channel的实作对象,可以使 ...
- xmlhttprequest upload
html5 带进度上传 function fileSelected() { var file = document.getElementById('fileToUpload').files[0]; i ...
- 如何用Procmon.exe来监视SQLSERVER的logwrite大小
如何用Procmon.exe来监视SQLSERVER的logwrite大小 在微软亚太区数据库技术支持组官方博客里面,你会发现很多篇文章都用到了Procmon.exe这个工具 今天我也介绍一下这个工具 ...
- vue-cli 3.0 开启 Gzip 方法
vue.config.js const path = require('path') const CompressionWebpackPlugin = require('compression-web ...
- 用于阻止缓冲区溢出攻击的 Linux 内核参数与 gcc 编译选项
先来看看基于 Red Hat 与 Fedora 衍生版(例如 CentOS)系统用于阻止栈溢出攻击的内核参数,主要包含两项: kernel.exec-shield 可执行栈保护,字面含义比较“绕”, ...
- Maven从私服上下载所需jar包——(十四)
1.修改settings.xml 将下面代码添加到settings.xml中 <profile> <!--profile的id--> <id>dev</id& ...
- robotium 中通过id获取 View 以及进行相应的操作
robotium 中id的几种表现形式 1)字符串形式:例如id/btn_example,源码中的布局里些的hierachy 中看见的都是这种. 2)数字形式:例如0x7f0700D,打开R.java ...
- 2、Centos6 安装tomcat8.5.31
1.下载 安装包 wget http://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar. ...
- Intellij IDEA调试功能总结
public class Demo { public static void f1() { System.out.println("one"); System.out.printl ...