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 ...
随机推荐
- spring源码学习之springMVC(二)
接着上一篇.继续来看springMVC中最和我们开发中接近的一部分内容: DispatcherServlet的逻辑处理 作者写到在DispatcherServlet类中存在doGet.doPost之类 ...
- 原生ajax请求json数据
<?php header("content-type:text/html;charset=utf-8"); echo '{"name":"小明& ...
- 一个页面多个bootstrip轮播以及一个页面多个swiper轮播 冲突问题
Bootstript轮播冲突 解决方法: 使用不同的id <div id="myCarousel1" class="carousel slide"> ...
- linux 修改命令提示符&ubuntu目录颜色个性化
1.linux 命令提示符个性化 在 ANSI 兼容终端(例如 xterm.rxvt.konsole 等)里, 可以用彩色显示文本而不仅仅是黑白. 本文示范了如何使用粗体和彩色的文字. 相信每 ...
- 状态压缩中常用的位运算(DP)
面对位运算,一直很无感...可能数学太差,脑洞太小. 1.首先是最基本的: 与&,或|,非~,异或^. 2.获取一个或者多个固定位的值: 假设 x = 1010(二进制),我们要取左数第二位的 ...
- 上传同步github
…or create a new repository on the command line echo "# testproject" >> README.md ...
- CSS-画三角
要实现三角的效果有很多方法: 例如下面的页面就需要三角: 其中, <!DOCTYPE html> <html lang="en"> <head> ...
- 你真的了解HTML吗
有这么一段HTML,请挑毛病: <P> 哥写的不是HTML,是寂寞.<br><br> 我说: <br>不要迷恋哥,哥只是一个传说 这是原来雅虎一道笔试题 ...
- NFS服务器搭建与配置
原文:https://blog.csdn.net/qq_38265137/article/details/83146421 NFS服务简介什么是NFS?NFS就是Network File System ...
- 用Direct2D和DWM来做简单的动画效果
原文:用Direct2D和DWM来做简单的动画效果 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sunnyloves/article/detail ...