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 -
思路:A:相同点数缩为一点,每个骨牌作为边连接两个点数,则每条边都必须走,这个时候根据欧拉迹的性质,找到度数为奇数的点作为起始点走一遍,或者没有奇数点随便找个点(此时欧拉回路),走一遍dfs遍历全图,看是否有边走不到(每个边只走一次)
WWA:错误原因:1 没有考虑到未联通的状态,直接判断必须是半欧拉图,这样只有一种点数的时候就错误
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef pair<int,int> P;
const int maxn=101;
int n,s,e;
int a[maxn][2];
vector <P> G[maxn];
int heap[maxn],len;
bool vis[maxn];
int deg[maxn];
int stck[2*maxn],slen;
bool used[2*maxn];
void dfs(int x){
for(int i=0;i<G[x].size();i++){//遍历从x出发能到达的点
if(!used[G[x][i].second]){
used[G[x][i].second]=true;//翻转和不翻转的状态都被遍历
used[G[x][i].second^1]=true;
dfs(G[x][i].first);//欧拉路径性质:从一个点出发可以到达所有点,也就是说如果这个点还有没走的边必然会回到这个点
stck[--slen]=G[x][i].second; }
}
}
void addedge(int from,int to,int ind){
G[from].push_back(P(to,2*ind));G[to].push_back(P(from,2*ind+1));
deg[from]++;deg[to]++;
}
int main(){
while(scanf("%d",&n)==1){
for(int i=0;i<maxn;i++)G[i].clear();//初始化
memset(used,0,sizeof(used));
memset(vis,0,sizeof(vis));
memset(deg,0,sizeof(deg));
len=slen=0;
for(int i=0;i<n;i++){
scanf("%d%d",a[i],a[i]+1);
if(!vis[a[i][0]]){heap[len++]=a[i][0];vis[a[i][0]]=true;}//去重
if(!vis[a[i][1]]){heap[len++]=a[i][1];vis[a[i][1]]=true;}
}
bool failed=false;s=e=-1;
for(int i=0;i<n;i++){//以牌为边加边
addedge(a[i][0],a[i][1],i);
}
for(int i=0;i<len;i++){
if(deg[heap[i]]&1){//奇数度点选作起点终点
if(s==-1)s=heap[i];
else if(e==-1)e=heap[i];
else failed=true;
}
}
if(failed){
puts("No solution");
}
else {
if(s==-1)s=heap[0];//欧拉回路任选一个作为起点
slen=n;
dfs(s);
if(slen!=0)puts("No solution");//有边没有遍历到
else for(int i=0;i<n;i++){
printf("%d ",stck[i]/2+1);
printf("%c\n",stck[i]&1?'-':'+');//状态2k为原来状态,2k+1则翻转
}
}
}
return 0;
}

  

sgu101 欧拉路径 难度:1的更多相关文章

  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 无向图的欧拉回路

    题目链接: https://cn.vjudge.net/problem/SGU-101 题目大意: 给定你n张骨牌,每张牌左右两端有一个数字,每张牌的左右两端数字可以颠倒,找出一种摆放骨牌的顺序,使得 ...

  3. P1341 无序字母对【欧拉路径】- Hierholzer模板

    P1341 无序字母对 提交 24.87k 通过 6.80k 时间限制 1.00s 内存限制 125.00MB 题目提供者yeszy 难度提高+/省选- 历史分数100 提交记录 查看题解 标签 福建 ...

  4. 【USACO 3.3】Riding The Fences(欧拉路径)

    题意: 给你每个fence连接的两个点的编号,输出编号序列的字典序最小的路径,满足每个fence必须走且最多走一次. 题解: 本题就是输出欧拉路径. 题目保证给出的图是一定存在欧拉路径,因此找到最小的 ...

  5. LL谱面分析和难度标定

    LL谱面分析和难度标定 先介绍一下LL谱面的存储方式:TimeLine序列(简称TL序列),TL序列中的每一个元素(即音符)可以由一个C语言中的结构体来表示: struct note{ int lin ...

  6. hdu 3472 HS BDC(混合路的欧拉路径)

    这题是混合路的欧拉路径问题. 1.判断图的连通性,若不连通,无解. 2.给无向边任意定向,计算每个结点入度和出度之差deg[i].deg[i]为奇数的结点个数只能是0个或2个,否则肯定无解. 3.(若 ...

  7. poj 2337 有向图输出欧拉路径

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 Descrip ...

  8. 关于完整解答Leo C.W博客中名为“我们公司的ASP.NET 笔试题,你觉得难度如何”的所有题目

    关于完整解答Leo C.W博客中名为“我们公司的ASP.NET 笔试题,你觉得难度如何”的所有题目,请大家鉴定,不足之处,敬请指教! 第1到3题解答如下: public enum QuestionTy ...

  9. nyoj 42 一笔画问题 欧拉路径

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=42 欧拉回路,欧拉路径水题~ 代码: #include "stdio.h&quo ...

随机推荐

  1. 关于STM32 MDK中USE_STDPERIPH_DRIVER问题的解释

    初学STM32,在RealView MDK 环境中使用STM32固件库建立工程时,初学者可能会遇到编译不通过的问题.出现如下警告或错误提示: warning: #223-D: function &qu ...

  2. poj 2481 Cows(树状数组)题解

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  3. Luogu P1314 聪明的质监员 二分答案

    题目链接 Solution 这个范围不是二分就是结论题就是数学题... 然后再看一会差不多就可以看出来有单调性所以就可以确定二分的解法了 二分那个$W$,用前缀和$O(n+m)$的时间来求出对答案的贡 ...

  4. nginx配置二级域名

    我在我的服务器上面跑了两个node应用程序,分别一个端口2368跑的是ghost博客,一个端口8000跑的是我的demo程序.想要一级域名zhangruojun.com用来访问博客,二级域名demo. ...

  5. [echats] - EChats图表的使用

    从上图可以看到,信息是能被抽象化为图形展示的,也就是基本的图表,曲线(想想股票那种曲线,普及一下那个叫K线图,想起当初去北京面试炒股公司的时候了...),柱状图等. 而apache开源的echats正 ...

  6. 【TCP/IP详解 卷一:协议】第十九章 TCP的交互数据流

    19.1 引言 前一章我们介绍了TCP连接的建立与释放:三握四挥,以及状态转移图. TCP报文段分为:交互数据,以及成块数据(下一章介绍). 交互数据:例如telnet,ssh,这种类型的协议在大多数 ...

  7. 【转载】可被路由的协议 & 路由协议 & 不可被路由的协议 的区别

    原文地址:可被路由的协议 & 路由协议 & 不可被路由的协议 的区别 术语routed protocol(可被路由的协议)和routing protocol(路由协议)经常被混淆.可被 ...

  8. Java zip解压,并遍历zip中的配置文件 .cfg或.properties

    1.解析cfg或properties配置文件 讲配置文件,读取,并封装成为map类型数据 /** * 解析cfg文件 * * @param cfgFile * @return */ public st ...

  9. React Native 的组件之底部导航栏 TabBarIOS(一)

    import React,{Component}from 'react'; import { AppRegistry, StyleSheet, Text, View, TabBarIOS, } fro ...

  10. Android广播接收器和Activity间传递数据

    Activity向广播接收器传递数据很简单,只需要在发送广播前将数据put进Intent中就行了. 广播接收器怎么向Activity传送数据?这里要用到接口,通过在广播接收器里定义一个接口,然后让接收 ...