SGU 101 Domino【欧拉路径】
题目链接:
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【欧拉路径】的更多相关文章
- SGU 101.Domino( 欧拉路径 )
求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...
- SGU 101 Domino (输出欧拉路径)
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...
- sgu 101 Domino 解题报告及测试数据
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求多米诺骨牌按照一定方式放置能否使相邻的位置 ...
- Domino - SGU 101 (欧拉路径)
题目大意:这是一个多米诺骨游戏,这个游戏的规则就是一个连着一个,现在给出 N 个多米诺,每个多米诺两边都有一个编号,相邻的多米诺的编号要一致,当然多米诺是可以翻转的(翻转就加‘-’,不翻转是‘+’), ...
- 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 ...
- SGU 101
SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...
随机推荐
- Django项目:CRM(客户关系管理系统)--36--28PerfectCRM实现King_admin编辑限制
#admin.py # ————————01PerfectCRM基本配置ADMIN———————— from django.contrib import admin # Register your m ...
- java并发系列(七)-----ConcurrentHashMap原理分析(JDK1.8)
JDK1.8的实现已经摒弃了Segment的概念,而是直接用Node数组+链表+红黑树的数据结构来实现,并发控制使用Synchronized和CAS来操作,整个看起来就像是优化过且线程安全的HashM ...
- linux 下 nginx 的最大文件打开设置
先设置好linux 系统的 limit, 见其他博文 nginx 也有自己的 limit 设置: 1. nginx.conf 设置 worker_rlimit_nofile 例如: worker_ ...
- Python的Django REST框架中的序列化及请求和返回
Python的Django REST框架中的序列化及请求和返回 序列化Serialization 1. 设置一个新的环境 在我们开始之前, 我们首先使用virtualenv要创建一个新的虚拟环境,以使 ...
- Python实例 包机制
每一个.py文件称为一个module,module之间可以互相导入.请参看以下例子: # a.py def add_func(a,b): return a+b # b.py from a im ...
- 极简bootstrap file 美化样式(无需第三方插件)
原本的file上传表单非常的丑,但是又不想使用第三方插件,Bootstrap也没有相关的美化,于是用纯CSS完成,美化,JS实现功能,连BootStrap都不需要,十分简单 1.给原版丑表单隐藏了di ...
- leetcode 1-20 easy
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- 2016年中国独角兽企业估值榜 TOP300
2016年中国独角兽企业估值榜 TOP300[完整榜单] 类型:品牌资讯/名企动态 阅读:6735次 来源: 中商情报网 我要评论 摘要:独角兽公司是什么?独角兽公司指的是那些估值达到10亿美元以 ...
- LintCode刷题笔记-- CoinsInLine
标签: 动态规划 问题描述: There are n coins with different value in a line. Two players take turns to take one ...
- channel补充
网易: package main import ( "fmt" ) func main() { var c chan int fmt.Printf("c=%v\n&quo ...