题目链接:

http://acm.sgu.ru/problem.php?contest=0&problem=101

题意:

N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转以调换其左右两数),求一种把这些骨牌从左到右排列的方案,使得所有相邻的两数字相等(即左边骨牌右侧的数字等于右边骨牌左侧的数字)。

分析:

把数字当成点,骨牌当做边。构成无向图,求一发欧拉道路即可。

无向图求欧拉路径还是很好写的。

欧拉路径深入讲解:http://blog.chinaunix.net/uid-26380419-id-3164913.html

代码:

#include<iostream>
#include<map>
#include<cstring>
#include<stack>
#include<set>
using namespace std;
const int maxn = 200 + 5, maxm = 6 + 5;
struct Edge{int to;int dir; int id;int next;};
int tot = 0;
Edge edge[maxn];
stack<Edge>s;
int pa[maxn], head[maxm], cnt[maxm];
bool vis[maxn];
int _find(int a)
{
if(a == pa[a]) return a;
return pa[a] = _find(pa[a]);
}
void unite(int a, int b)
{
int ra = _find(a);
int rb = _find(b);
if(ra == rb) return ;
pa[rb] = ra;
}
bool same(int a, int b)
{
return _find(a) == _find(b);
}
void add_edge(int u, int v, int id)
{
edge[tot].to = v;
edge[tot].id = id;
edge[tot].dir = 1;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].to = u;
edge[tot].dir = 0;
edge[tot].id = id;
edge[tot].next = head[v];
head[v] = tot++;
}
void dfs(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next){
if(!vis[i]){
vis[i] = true;
vis[i^1] = true;
dfs(edge[i].to);
s.push(edge[i]);
}
}
}
int main (void)
{
int n ;cin>>n;
memset(head, -1, sizeof(head));
for(int i = 0; i <= 6; i++) pa[i] = i;
int a, b;
for(int i = 0; i < n; i++){
cin>>a>>b;
add_edge(a, b, i + 1);
cnt[a]++;
cnt[b]++;
unite(a, b);
}
int be = -1;
int ans = 0;
for(int i = 0; i <= 6; i++){
if(cnt[i] & 1){
ans++;
if(be == -1) be = i;
}
}
if(ans != 0 && ans != 2) return cout<<"No solution"<<endl, 0;
for(int i = 0; i <= 6; i++){
if(cnt[i] && be == -1) be = i;
if(cnt[i] && !same(i, be)){return cout<<"No solution"<<endl, 0;}
}
dfs(be);
while(!s.empty()){
Edge t = s.top();s.pop();
cout<<t.id<<' ';
if(t.dir == 0) cout<<"-"<<endl;
else cout<<"+"<<endl;
}
return 0;
}

SGU 101 Domino【欧拉路径】的更多相关文章

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

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

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

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

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

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

  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. [Array]283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  2. TZ_06_SpringMVC_异常处理,自定义异常

    1.SpringMVC异常处理的方式 . 2. 异常处理思路 1>. Controller调用service,service调用dao,异常都是向上抛出的,最终有DispatcherServle ...

  3. mybatis深入理解(三)-----MyBatis事务管理机制

    MyBatis作为Java语言的数据库框架,对数据库的事务管理是其非常重要的一个方面.本文将讲述MyBatis的事务管理的实现机制.首先介绍MyBatis的事务Transaction的接口设计以及其不 ...

  4. spring springmvc 展示图片,静态资源的处理

    jsp中显示一张照片 <img alt="静态图片" src="static/目录.png"> 然后在springmvc的配置中加上 <!-- ...

  5. Oracle查询表里的重复数据方法

    select id from group by id having count(*) > 1 按照id分组并计数,某个id号那一组的数量超过1条则认为重复. 如何查询重复的数据 select 字 ...

  6. JS常用属性方法大全

    1. 输出语句 : document.write(""); 2.JS 中的注释为 : // 3. 传统的 HTML 文档顺序是 : document->html->(h ...

  7. 错觉-Info:视错觉与UI元素间的可能

    ylbtech-错觉-Info:视错觉与UI元素间的可能 1.返回顶部 1. 视觉原理在当下红火的机械视觉中是必不可少的,那在我们日常工作的UI产品设计中又有什么可能性的呢?今天,我从“视错觉”这个角 ...

  8. TypeScript高级类型

    交叉类型 将多个类型合并成一个类型,去两个类型的并集.与继承的区别是,继承可以有自己的属性,而交叉没有. interface DogInterface { run():void } interface ...

  9. 开源中国 ThinkPHP 领奖

    开源中国 ThinkPHP 的领奖 周日早上早早就起来参考开源中国的活动. 由于今年竞争激烈 FastAdmin 没有上榜,但是没关系,因为这说明整个开源环境越来越好了,对于我们来说是利好. 因为 T ...

  10. Leetcode860.Lemonade Change柠檬水找零

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...