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 ...
随机推荐
- 20155210潘滢昊 2016-2017-2 《Java程序设计》第6周学习总结
20155210 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 流(Stream)是对「输入输出」的抽象,注意「输入输出」是相对程序而言的 InputStr ...
- 【腾讯云】自己搭建的腾讯云服务器JavaEE环境
0.安装SSH登录 1.生成公钥对 ssh-keygen -t rsa -P '' -P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车.它在/home/ch ...
- MapReduce--shuffle
一.Shuffle简介 Shuffle的正常意思是洗牌或弄乱,shuffle的大致范围就是把map task的输出结果有效的传送到Reduce端.也可以这样理解,shuffle描述着数据从map ta ...
- linux命令中which、whereis、locate有什么区别?
1.find find是最常用和最强大的查找命令.它能做到实时查找,精确查找,但速度慢. find的使用格式如下: #find [指定目录] [指定条件] [指定动作] 指定目录:是指所要搜索的目录和 ...
- linux下热插拔事件的产生是怎样通知到用户空间,kobject_uevent_env之uevent【转】
转自:http://blog.csdn.net/myarrow/article/details/8259888 1.kobject, ktype, kset 1) kobject: 代表sysfs中的 ...
- 首发:极简的Centos主机监控方法,分分钟即可使用【转】
需求天天有,今年事更多.硬盘测试刚刚完成,就又来了性能监控的需求.一般我们生产就用zabbix了,用起来还行,就是蛮多脚本要写.开发和测试都是分散的,经常还要重装系统,用zabbix就算了,开发和测试 ...
- CasperJS API中文博客链接
http://www.cnblogs.com/reach296/tag/Casperjs/
- centos7切换启动内核
1.查看当前系统内核 uname -r 3.18.6-2.el7.centos.x86_64 查看可使用的内核列表 awk -F\' '$1=="menuentry " {prin ...
- spring-web.xml 模板
ssm模板 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- Ubuntu下pycharm安装
参考:http://blog.csdn.net/langb2014/article/details/51166782 http://www.cnblogs.com/zhcncn/p/4027025.h ...