sgu101-欧拉回路
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 -
题意是这种:给你一个数字N,告诉你有多少牌,接下来每一行分别代表一张牌,两个数字分别代表正反两面。能够交换数字。比方我是[1,2]。[3。2]那么我就能够先推倒[1,2],然后转下[3,2]变成[2。3]那么就能够推倒[2,3]
如题目sample,output第一行是2 -,那么我就先选第2个[2,4]变成[4,2],然后是5+,就是[2,1],依次下去就能够所有推倒了。当然有些组合是能够推倒有些是不能所有推倒的。
怎么reduce这个题呢?应该非常easy想到一笔画问题。真的是非常easy想到 = =,那么sample input 就转化成watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJjZDE5OTI3MTln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="300" height="130" alt="">
就是一个无向欧拉通路(跟回路差点儿相同。仅仅是回路要回到起点)问题
仅仅要奇数度的点的个数为0或者2就能够达成通路,假设是0,那么起点随意选,否则起点要选奇数度点。
所以我们要用DFS
void through(int start, vector<int>& s){
vector<int>e = maps[start];
for(int i = 0;i < e.size();i++){
if(Edges[e[i]].visited)
continue;
else{
Edges[e[i]].visited = true;
if(start == Edges[e[i]].v1){
through(Edges[e[i]].v2, s);
s.push_back(e[i]);
} else {
through(Edges[e[i]].v1, s);
s.push_back(e[i]);
}
}
}
return;
}最核心的代码在这里,在DFS里有个跟出栈差点儿相同的操作,在这个操作后,我们就把这条路径给加到结果里面去,当然结果里面是逆序的,我们要逆向输出
以下贴出所有代码
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <map> using namespace std; int N = 0;
int occ[7] = {0};
int START = 0; struct Edge{
int v1,v2;
bool visited;
Edge(){};
Edge(int _v1, int _v2, bool _vis){
v1 = _v1;v2 = _v2;visited = _vis;
}
};
struct Edge Edges[110];
map< int , vector<int> >maps; /*
* 返回一个节点的全部边
*/
vector<int> getEdge(int n){
vector<int> set;
for(int i = 0;i < N;i++){
if(n == Edges[i].v1 || n == Edges[i].v2){
set.push_back(i);
}
}
return set;
} void res(int start,vector<int>& s){
for(int i = s.size()-1;i >= 0;i--){
int front = Edges[s[i]].v1;
int back = Edges[s[i]].v2;
if(start == front){
cout << s[i]+1 << " +" << endl;
start = back;
} else {
cout << s[i]+1 << " -" << endl;
start = front;
}
}
} void through(int start, vector<int>& s){
vector<int>e = maps[start];
for(int i = 0;i < e.size();i++){
if(Edges[e[i]].visited)
continue;
else{
Edges[e[i]].visited = true;
if(start == Edges[e[i]].v1){
through(Edges[e[i]].v2, s);
s.push_back(e[i]);
} else {
through(Edges[e[i]].v1, s);
s.push_back(e[i]);
}
}
}
return;
} int main()
{
int front,back;
cin >> N; int index = 0;
while(cin >> front && cin >> back)
{
Edges[index] = Edge(front,back,false);
index++;
occ[front]++;
occ[back]++;
} int odd_pot = 0, start = Edges[0].v1;
for(int i = 0;i < 7;i++){
if(occ[i] % 2 == 1){
odd_pot++;
start = i;
}
maps[i] = getEdge(i);
}
START = start;
if(odd_pot != 0 && odd_pot != 2){
cout << "No solution";
return 0;
} vector<int>results;
through(start,results);
if(results.size() < N)cout << "No solution";
else res(START,results); return 0;
}
sgu101-欧拉回路的更多相关文章
- SGU---101 无向图的欧拉回路
题目链接: https://cn.vjudge.net/problem/SGU-101 题目大意: 给定你n张骨牌,每张牌左右两端有一个数字,每张牌的左右两端数字可以颠倒,找出一种摆放骨牌的顺序,使得 ...
- ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)
//网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- UVA 10054 the necklace 欧拉回路
有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...
- POJ 1637 混合图的欧拉回路判定
题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- UVa 12118 检查员的难题(dfs+欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10054 (欧拉回路) The Necklace
题目:这里 题意:有一种由彩色珠子连接而成的项链,每个珠子两半由不同颜色(由1到50的数字表示颜色)组成,相邻的两个珠子在接触的地方颜色相同,现在有一些零碎的珠子,确认它是否能 复原成完整的项链. 把 ...
- poj2513Colored Sticks(无向图的欧拉回路)
/* 题意:将两端涂有颜色的木棒连在一起,并且连接处的颜色相同! 思路:将每一个单词看成一个节点,建立节点之间的无向图!判断是否是欧拉回路或者是欧拉路 并查集判通 + 奇度节点个数等于2或者0 */ ...
随机推荐
- AC日记——Dining poj 3281
[POJ-3281] 思路: 把牛拆点: s向食物连边,流量1: 饮料向t连边,流量1: 食物向牛1连边,流量1: 牛2向饮料连边,流量1: 最大流: 来,上代码: #include <cstd ...
- Python的网络编程[4] -> DHCP 协议[0] -> DHCP 的基本理论
DHCP协议 / DHCP Protocol 目录 DHCP 基本理论 DHCP 通信流程 DHCP 完整报文 DHCP 的 Optional 字段 DHCP 的报文类型 1 DHCP 基本理论 DH ...
- 在apache2.4.6中配置虚拟主机支持web.py
web.py 是一个简单好用的python web框架. (http://webpy.org/) apache httpd是一款开源配置简单的web容器. (http://apache.org/) 假 ...
- vue的路由设置小结
vue的路由设置小结 // 异步路由的编写示例.其中针对component字段进行懒加载及分块处理,提升首屏加载速度的同时,也可以手动控制让某些页面合并到一个单独的js文件中,而不是每个页面都是一个j ...
- 「kuangbin带你飞」专题十五 数位DP
传送门 A.CodeForces - 55D Beautiful numbers 题意 一个正整数是 漂亮数 ,当且仅当它能够被自身的各非零数字整除.我们不必与之争辩,只需计算给定范围中有多少个漂亮数 ...
- Oracle PL/SQL入门之慨述
Oracle PL/SQL入门之慨述 一.PL/SQL出现的目的 结构化查询语言(Structured Query Language,简称SQL)是用来访问关系型数据库一种通用语言,它属于第四代语言( ...
- uprobes issue with oracle 12c
https://mahmoudhatem.wordpress.com/2017/03/21/uprobes-issue-with-oracle-12c/
- tiny4412 串口驱动分析七 --- log打印的几个阶段之内核启动阶段(earlyprintk)
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...
- MathType requires a newer version of MT Extra等MathType问题的不兼容性解决方案
常见问题解决方法: 1.MathType 6.0与office 2007兼容问题 由于Office软件安装时默认是不安装公式编辑器的,在安装完MathType 6.0之后,需要将\MathType 6 ...
- [置顶]
kubernetes资源对象--Horizontal Pod Autoscaling(HPA)
概念 HPA全称Horizontal Pod Autoscaling,即pod的水平自动扩展.自动扩展主要分为两种,其一为水平扩展,针对于实例数目的增减:其二为垂直扩展,即单个实例可以使用的资源的增减 ...