题目链接:

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. Django项目:CRM(客户关系管理系统)--26--18PerfectCRM实现King_admin搜索关键字

    search_fields = ('name','qq',) 登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html search_fields = ( ...

  2. 洛谷 2890 [USACO07OPEN]便宜的回文Cheapest Palindrome

    传送门 一道最简单的区间dp,然而我还是抄了题解. //Twenty #include<algorithm> #include<iostream> #include<cs ...

  3. [转]JS设计模式-单例模式(二)

    单例模式是指保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式是一种常用的模式,有一些对象往往只需要一个,比如线程池.全局缓存.浏览器中的window对象等.在javaScript开发中 ...

  4. 读书笔记--Head First C#目录

    1.c#助你快速开发2.都只是代码3.对象4.类型与引用5.封装6.继承7.接口与抽象类8.枚举与集合9.读/写文件10.异常处理11.事件与委托12.复习与预习13.控件与图片14.captain ...

  5. php解决高并发(文件锁)

    文件锁分为两种方式: [一].阻塞模式:(如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行) <?php //连接数据库 $con=mysqli_connect(" ...

  6. 凸优化 & 1概念

    ---恢复内容开始--- 放射集合 系数之和为1 相加仍然能在集合内,就是 纺射集合 子空间加一个常熟 就是纺射集合 , 例题2.1 一类特殊的线性方程组的解可以看作纺射 集合 纺射包 aff C 是 ...

  7. 2019.7.27 NOIP模拟测试9 反思总结

    先来整理题目 T1题目大意:给出n个数字和一个质数作为模数,一个变量x初始值为1.进行m次操作,每次让x随机乘上n个数中的一个,问m次操作以后x的期望值. 答案一定可以用分数表示,输出分子乘分母逆元的 ...

  8. day38 06-MyEclipse配置Schema约束

  9. django中的路由规则

    项目的目录结构 外层的firstysite目录与Django无关,只是你项目的容器,可以任意重命名. #其中settings.py是django的总配置文件,即项目的总管家#urls.py,则是项目的 ...

  10. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...